之前看到過好多關於PHP匯出Excel的文章,自己也通過不少方法實現過,
例如,echo一個Table,然後將header修改為Excel即可。
其實在眾多方法中感覺Pear中的Spreadsheet_Excel_Writer很是好用。
關於Spreadsheet_Excel_Writer的安裝本文不予介紹,可通過Google找到
答案:) 也可參考: http://pear.php.net/package/Spreadsheet_Excel_Writer/download
<?php
//調用Writer.php
require_once 'Spreadsheet/Excel/Writer.php';
//建立Workbook
$workbook = new Spreadsheet_Excel_Writer();
//定義匯出Excel檔案名稱
$workbook->send('test.xls');
//建立Worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');
//設定字型大小
$format_column = & $workbook->addformat(array('Size'=>9,'Bold'=>1));
//資料寫入
//標題列(第一行)
$worksheet->write(0, 0, 'Name', $format_column);
$worksheet->write(0, 1, 'Age', $format_column);
//第一個人(第二行)
$worksheet->write(1, 0, 'John Smith');
$worksheet->write(1, 1, 30);
//第二個人(第三行)
$worksheet->write(2, 0, 'Johann Schmidt');
$worksheet->write(2, 1, 31);
//第三個人(第四行)
$worksheet->write(3, 0, 'Juan Herrera');
$worksheet->write(3, 1, 32);
//關閉Workbook
$workbook->close();
?>
:
當然,Spreadsheet_Excel_Writer的設定遠遠不止這些,詳細介紹請參考:
http://pear.php.net/package/Spreadsheet_Excel_Writer/docs