之前看到過好多關於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();
?>
效果圖: