PHP Excel, but no Spreadsheet_Excel_Writer
I need to save some data in .xls file. I am trying to use PEAR library Spreadsheet_Excel_Writer. It is work. But now i am working with hosting without PEAR. Of cause, it is very terrible.
Spreadsheet_Excel_Writer has a lot of dependencies. I need a module or code, which i can include in 开发者_运维问答my scripts.
Thx.
I use Spreadsheet Writer a lot and it's nice to have, but if you don't need multiple sheets or the ability to do some formatting, etc, you an always just output HTML and send a
header("Content-type: application/vnd.ms-excel")
and an extension of ".xls" and then Excel or OpenOffice's spreadsheet program (and I assume, I guess, Mac's spreadsheet program) will open it and format the HTML as a spreadsheet.
It's not a native solution but in the end, it looks pretty good to the end-user. You have to use tables in your html but you can add colors/borders, padding, font changes, rowspan, colspan, etc, for a bit of control. No functions or formulas or multiple sheets, though.
It's a little-known format that never really caught on, but MS Office has support for so-called Excel 2003 XML files - which are indeed XML and, at a bare minimum, look like this:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet>
<Row>
<Cell>
<Data ss:Type="String">First</Data>
</Cell>
</Row>
</Worksheet>
</Workbook>
You could generate this quite simply without any PHP extensions, and it will open correctly in any version of MS Excel since 2003. OpenOffice 2 and 3 will open these correctly if you rename the file extension to .xls.xml
When a user opens this file and saves it from Excel, it will be, by default, converted to the classic XLS file.
As @Alexander.Plutov notes in a comment, there is a library for writing these files.
There's a useful (and more comprehensive) page on IBM's site about this format and PHP: http://www.ibm.com/developerworks/opensource/library/os-phpexcel/#N101F7
Literally, PHPExcel: http://phpexcel.codeplex.com/
Im using http://www.bettina-attack.de/jonny/view.php/projects/php_writeexcel/
I got it simply included in many projects and it works like a charm.
Beware, the source code provided by Piskvor:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet>
<Row>
<Cell>
<Data ss:Type="String">First</Data>
</Cell>
</Row>
</Worksheet>
</Workbook>
is not a valid Excel file (at least in my version – Excel 2010) and will fail opening producing an error message, as it is missing two required values corrected in the following example (the missing part is that within a WorkSheet
you need a Table
and a WorkSheet
must have a name specified as a string in ss:Name
):
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="test">
<Table>
<Row>
<Cell>
<Data ss:Type="String">First</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
This then becomes a valid excel spreadsheet indeed and thanks to Piskvor for reminding us of this wonderful open and machine- and human-readable format.
精彩评论