nuovo / spreadsheet-reader

A PHP spreadsheet reader (Excel XLS and XLSX, OpenOffice ODS, and variously separated text files) with a singular goal of getting the data out, efficiently
http://www.nuovo.lv/
Other
674 stars 498 forks source link

"Call to a member function read() on a non-object" due to wrong sheetId #119

Open vandroids opened 8 years ago

vandroids commented 8 years ago

When i save XLSX document in LibreOffice Calc, this reader cannot read the document.

Example document where workbook.xml file contains following data: <sheets> <sheet name="MyData" sheetId="1" state="visible" r:id="rId2"/> </sheets>

As you see here sheetId = "1", and relation id is "rId2"

SpreadsheetReader_XLSX.php contains method Sheets() with a code:

$Attributes = $Sheet -> attributes('r', true);
foreach ($Attributes as $Name => $Value)
{
    if ($Name == 'id')
    {
        $SheetID = (int)str_replace('rId', '', (string)$Value);
        break;
    }
}

This code is wrong for obvious reason. You cannot make an assumption that r:id attribute always contains correct sheet ID at the end, in fact r:id can contain any random string.

The best option to solve this issue would be to rewrite Sheets() method so that it will look up correct sheet in a file called workbook.xml.rels. In my example workbook.xml.rels file looke like this: <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"&gt; <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/> </Relationships>

TonisOrmisson commented 8 years ago

https://github.com/andmemasin/spreadsheet-reader/commit/7fe9ff5db07ed1bb2ff4544b3b1ba6ec5872f2d3