mk-j / PHP_XLSXWriter

Lightweight XLSX Excel Spreadsheet Writer in PHP
MIT License
1.84k stars 665 forks source link

Error with indices of sheets #272

Closed FoGezz closed 1 year ago

FoGezz commented 4 years ago

Hello. I create file with xlsxwriter and then read it with https://github.com/nuovo/spreadsheet-reader. I have encountered a problem that when i start reading file (it has 2 sheets) the default is the second. But it worked well when i changed $i+2 to $i+1 at the line below $workbook_xml.='<sheet name="'.self::xmlspecialchars($sheet->sheetname).'" sheetId="'.($i+1).'" state="visible" r:id="rId'.($i+2).'"/>';

xlsxwriter.class.php -> buildWorkBookXML(); What's purpose of using r:id="rId'.($i+2)? Is it correct? I couldnt find any information about that in OpenXML docs. And I cant override that implementation because of using self:: when calling

UPD: here is working code at the last version (i did it like to Excel app saves)


    {
        $i=0;
        $workbook_xml="";
        $workbook_xml.='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n";
        $workbook_xml.='<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">';
        $workbook_xml.='<fileVersion appName="Calc"/><workbookPr backupFile="false" showObjects="all" date1904="false"/><workbookProtection/>';
        $workbook_xml.='<bookViews><workbookView activeTab="0" firstSheet="0" showHorizontalScroll="true" showSheetTabs="true" showVerticalScroll="true" tabRatio="212" windowHeight="8192" windowWidth="16384" xWindow="0" yWindow="0"/></bookViews>';
        $workbook_xml.='<sheets>';
        foreach($this->sheets as $sheet_name=>$sheet) {
            $sheetname = self::sanitize_sheetname($sheet->sheetname);
            $workbook_xml.='<sheet name="'.self::xmlspecialchars($sheetname).'" sheetId="'.($i+1).'" state="visible" r:id="rId'.($i+1).'"/>';
            $i++;
        }
        $workbook_xml.='</sheets>';
        $workbook_xml.='<definedNames>';
        foreach($this->sheets as $sheet_name=>$sheet) {
            if ($sheet->auto_filter) {
                $sheetname = self::sanitize_sheetname($sheet->sheetname);
                $workbook_xml.='<definedName name="_xlnm._FilterDatabase" localSheetId="0" hidden="1">\''.self::xmlspecialchars($sheetname).'\'!$A$1:' . self::xlsCell($sheet->row_count - 1, count($sheet->columns) - 1, true) . '</definedName>';
                $i++;   
            }
        }
        $workbook_xml.='</definedNames>';
        $workbook_xml.='<calcPr iterateCount="100" refMode="A1" iterate="false" iterateDelta="0.001"/></workbook>';
        return $workbook_xml;
    }
    protected function buildWorkbookRelsXML()
    {
        $i=0;
        $wkbkrels_xml="";
        $wkbkrels_xml.='<?xml version="1.0" encoding="UTF-8"?>'."\n";
        $wkbkrels_xml.='<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">';
        foreach($this->sheets as $sheet_name=>$sheet) {
            $wkbkrels_xml.='<Relationship Id="rId'.($i+1).'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/'.($sheet->xmlname).'"/>';
            $i++;
        }
        $wkbkrels_xml.='<Relationship Id="rId'.($i+1).'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>';
        $wkbkrels_xml.="\n";
        $wkbkrels_xml.='</Relationships>';
        return $wkbkrels_xml;
    }```
mk-j commented 1 year ago

rId+2 is trying to make the Relationship Ids and files line up properly, it sounds like it works correctly most of the time, just not in your situation... glad you figured it out and made a fix for your situation.