phpexcelpackspreadsheet-excel-writer

File is not readable with Excelwriter and phpExcelReader 2


I use Excel Writer of Harish Chauhan to generate an excel (xls) file.

Then I use phpExcelReader 2 to read the file created by the Excel Writer class but have this error all the time :

The filename myXls.xls is not readable

I can open the "myXls.xls" file with MS Excel. But if I save the file with another name , it can be read successfully.

Try to explore the code, it seems that the error was given by :

if (substr($this->data, 0, 8) != IDENTIFIER_OLE) {
    //echo 'Error';
    $this->error = 1;
    return false;
}

IDENTIFIER_OLE was defined :

define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));

I dont have any idea about how to fix it. Please help.

Thanks for your time!


Solution

  • The file generated by Harish Chauhan's ExcelWriter class is not an actual OLE BIFF .xls file, but a mix of HTML markup and some elements from SpreadSheetML, the XML format defined by Microsoft as an alternative to BIFF in Excel 2003. It never proved particularly popular; but the later versions of MS Excel itself can still read and write this format. MS Excel is also very forgiving about reading HTML markup, though the latest version will give you a notice informing you if a file format does not match its extension.

    phpExcelReader 2 is written to read Excel BIFF files, therefore it is incapable of reading the non-OLE/non-BIFF files generated by Harish Chauhan's class.

    If you want to write and read files in the correct format, then I suggest you use PHPExcel, or one of the many other PHP libraries that work with genuine Excel files.