PHPOffice / PHPExcel

ARCHIVED
Other
11.46k stars 4.19k forks source link

Read problems with generated files from the internet #571

Open arjen8 opened 9 years ago

arjen8 commented 9 years ago

Recently I downloaded a generated Excel file (xlsx) from the internet, and saved it to my pc. To edit this file in Excel, I first have to click on 'Protected view - Enable editing' to edit the document.

When I upload the file to the server without opening this file in Excel first, PHPExcel isn't able to recognize the data in the Excel file. After opening the file in Excel and clicking on 'Enable editing' and re-saving the file, PHPExcel imports the file correctly.

arjen8 commented 9 years ago

I think I figured it out what causes the problem. When I open the xlsx archive and looked at, for example, the file 'workbooks.xml' the following code is included in the generated xlsx file from the internet:

<?xml version="1.0" encoding="utf-8"?><x:workbook xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:sheets><x:sheet name="Sheet1" sheetId="1" r:id="Rbcb16ba2d8a34231" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" /></x:sheets></x:workbook>

Your code is looking for the node 'sheets' and couldn't find any matches because of the 'x' namespace. If I remove the 'x' namespace and do a check with the code below, the list of worksheets is correct (earlier it wasn't).

<?php var_dump($objReader->listWorksheetNames($file_name)); ?>

It doesn't fix the problem completely, but maybe this helps you a little bit.

motin commented 8 years ago

I found a workaround, it is ugly but works until this issue has been fixed...

Create a new class PHPExcel_Reader_Excel2007_XNamespace.php:

<?php

class PHPExcel_Reader_Excel2007_XNamespace extends PHPExcel_Reader_Excel2007
{

    public function securityScan($xml)
    {
        $xml = parent::securityScan($xml);
        return str_replace(['<x:', '</x:'], ['<', '</'], $xml);
    }

}

Then load your excel file using this reader:

$excelReader = new PHPExcel_Reader_Excel2007_XNamespace();
$objPHPExcel = $excelReader->load($inputFileName);
adoerler commented 7 years ago

Hey,

today we faced this problem in one of our projects. XLSX files generated form Microsoft Dynamics CRM are namespaced, loading them in PHPExcel will lead to an empty dataset (one worksheet, with no contents). This workaround fixed the problem for us - thank you!

Are there any plans to integrate a solution for reading namespaced XLSX files in one of the upcomming releases?

Kind regards Andreas

JosueMorales commented 7 years ago

This workaround works like a charm!

Thank you!