-1

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: enter image description here

Does anyone knows what could be wrong? Thank you.

F0l0w
  • 243
  • 5
  • 15
  • 1
    Possible duplicate of [How can i write data into an excel using PHP](https://stackoverflow.com/questions/3968973/how-can-i-write-data-into-an-excel-using-php) – mikeb Jan 05 '18 at 01:47
  • I dont want to use PHPExcel Library... – F0l0w Jan 05 '18 at 01:47
  • 1
    xlsx is a very complexe format. It's not just plain text. Maybe a CSV format will work better for you – Ibu Jan 05 '18 at 01:50
  • You don't have to use PHPExcel - you can write XML instead -> https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats – mikeb Jan 05 '18 at 01:51

1 Answers1

0

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);
mikeb
  • 10,578
  • 7
  • 62
  • 120
  • Can you provide me a very simple example of using the XML format for Excel? I would sincerily appreciate it – F0l0w Jan 05 '18 at 01:55