How can I write data into an excel using PHP?
Is it possible to append c开发者_Go百科ontent to an .xls file using PHP fwrite()
?
When I try this using fwrite()
, the resulting file causes an error message in Excel 2007.
Is there a specific separator I can use to accomplish this?
Is it possible without a third party library?
You can use the PhpSpreadsheet library, to read an existing Excel file, add new rows/columns to it, then write it back as a real Excel file.
Disclaimer: I am one of the authors of this library.
You can try and create a CSV file, like this:
name;surname;blabla
name;surname;blabla
name;surname;blabla
name;surname;blabla
Excel should eat this :)
It is convenient to use PHP CVS functions: http://php.net/manual/en/function.fputcsv.php
to write you can use : Spreadsheet_Excel_Writer is a tool for creating Excel files without the need for COM components
http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php but you cant appaend to file , only to create it.
or using phpexcel (support excel 2007)
http://phpexcel.codeplex.com/
and you can append see a example :
http://phpexcel.codeplex.com/Thread/View.aspx?ThreadId=82996
use from fputcsv function for example :
$data[] = array("item 1", "item 1");
$export = fopen("file.csv", "w");
foreach ($data as $row) {
fputcsv($export, $row, "\t");
}
fclose($export);
for more example : https://www.php.net/manual/en/function.fputcsv.php
if you just want/need to create a very simple table, you just need to make a *.csv-file which can be opened by excel - but not: you can't use formulas in this and you can't do any kind of formatting.
you can try this library http://phpexcel.codeplex.com/
or you can create .csv file for example and then import them to excel.
I wrote a simple library for exporting Excel-friendly XML files from PHP: http://github.com/elidickinson/php-export-data
精彩评论