PHP fwrite “.xlsx format or extension is not valid

2019-09-21 06:51发布

问题:

I am trying to write data into an excel file. I execute the code, but when I then try to open the excel file, I am getting an error that states that the format or the extension are not valid. This is my first time working with excel files on PHP.

Here is my code:

$e = fopen("Test.xlsx", "w");
fwrite($e, $balances['XVG']['onOrder']);
fclose($e);

Excel 2013 version:

Does anyone knows what could be wrong? Thank you.

回答1:

You can use PHPExcel:

How can i write data into an excel using PHP

You can also use Microsoft's XML format for Excel:

https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats

To create a one row spreadsheet with a header, you would write the following XML to a file:

$xml = <<<END_XML_ZZ
    <?xml version="1.0" encoding="UTF-8"?>
    <?mso-application progid="Excel.Sheet"?>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
    <Worksheet ss:Name="CognaLearn+Intedashboard">
    <Table>
    <Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="110"/>
    <Row>
    <Cell><Data ss:Type="String">ID</Data></Cell>
    <Cell><Data ss:Type="String">Project</Data></Cell>
    <Cell><Data ss:Type="String">Reporter</Data></Cell>
    <Cell><Data ss:Type="String">Assigned To</Data></Cell>
    <Cell><Data ss:Type="String">Priority</Data></Cell>
    <Cell><Data ss:Type="String">Severity</Data></Cell>
    <Cell><Data ss:Type="String">Reproducibility</Data></Cell>
    <Cell><Data ss:Type="String">Product Version</Data></Cell>
    <Cell><Data ss:Type="String">Category</Data></Cell>
    <Cell><Data ss:Type="String">Date Submitted</Data></Cell>
    <Cell><Data ss:Type="String">OS</Data></Cell>
    <Cell><Data ss:Type="String">OS Version</Data></Cell>
    <Cell><Data ss:Type="String">Platform</Data></Cell>
    <Cell><Data ss:Type="String">View Status</Data></Cell>
    <Cell><Data ss:Type="String">Updated</Data></Cell>
    <Cell><Data ss:Type="String">Summary</Data></Cell>
    <Cell><Data ss:Type="String">Status</Data></Cell>
    <Cell><Data ss:Type="String">Resolution</Data></Cell>
    <Cell><Data ss:Type="String">Fixed in Version</Data></Cell>
    </Row>
    <Row>
    <Cell><Data ss:Type="Number">0000033</Data></Cell>
    <Cell><Data ss:Type="String">CognaLearn Intedashboard</Data></Cell>
    <Cell><Data ss:Type="String">janardhana.l</Data></Cell>
    <Cell><Data ss:Type="String"></Data></Cell>
    <Cell><Data ss:Type="String">normal</Data></Cell>
    <Cell><Data ss:Type="String">text</Data></Cell>
    <Cell><Data ss:Type="String">always</Data></Cell>
    <Cell><Data ss:Type="String"></Data></Cell>
    <Cell><Data ss:Type="String">GUI</Data></Cell>
    <Cell><Data ss:Type="String">2016-10-14</Data></Cell>
    <Cell><Data ss:Type="String"></Data></Cell>
    <Cell><Data ss:Type="String"></Data></Cell>
    <Cell><Data ss:Type="String"></Data></Cell>
    <Cell><Data ss:Type="String">public</Data></Cell>
    <Cell><Data ss:Type="String">2016-10-14</Data></Cell>
    <Cell><Data ss:Type="String">IE8 browser_Modules screen tool tip text is shown twice</Data></Cell>
    <Cell><Data ss:Type="String">new</Data></Cell>
    <Cell><Data ss:Type="String">open</Data></Cell>
    <Cell><Data ss:Type="String"></Data></Cell>
    </Row>
    </Table>
    </Worksheet>
    </Workbook>

END_XML_ZZ;
$e = fopen("Test.xlsx", "w");
fwrite($e, $xml);
fclose($e);


标签: php excel fwrite