PHPExcel icon indicating copy to clipboard operation
PHPExcel copied to clipboard

Read problems with generated files from the internet

Open arjen8 opened this issue 9 years ago • 4 comments

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 avatar May 21 '15 10:05 arjen8

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).

listWorksheetNames($file_name)); ?>

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

arjen8 avatar May 21 '15 11:05 arjen8

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);

motin avatar May 02 '16 19:05 motin

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

adoerler avatar Apr 13 '17 18:04 adoerler

This workaround works like a charm!

Thank you!

JosueMorales avatar Sep 06 '17 15:09 JosueMorales