PHP fwrite“.xlsx格式或扩展名无效”本地服务器

时间:2018-01-05 01:45:37

标签: php excel fwrite

我正在尝试将数据写入excel文件。我执行代码,但是当我尝试打开excel文件时,我收到一条错误,指出格式或扩展名无效。这是我第一次在PHP上使用excel文件。

这是我的代码:

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

Excel 2013版: enter image description here

有谁知道什么可能是错的?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用PHPExcel:

How can i write data into an excel using PHP

您还可以将Microsoft的XML格式用于Excel:

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

要创建带标题的单行电子表格,您可以将以下XML写入文件:

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