設置 Content-Type: application/vnd.ms-excel 響應頭,輸出帶內聯樣式的 HTML <table>,Excel 打開時會自動渲染樣式。
public function export() {
$fileName = "成績單-" . date("Y-m-d_H-i-s");
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename={$fileName}.xls");
header("Cache-Control: max-age=0");
// 此處可進行數據查詢、模板賦值
$this->display(); // 輸出 HTML 模板
}
export.html<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="https://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ProgId" content="Excel.Sheet">
<meta name="Generator" content="Microsoft Excel 11">
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="5" align="center"><h2>成績單</h2></td>
</tr>
<tr style="background:#4472C4; color:white; font-weight:bold;">
<td style="width:54pt" align="center">編號</td>
<td style="width:54pt" align="center">姓名</td>
<td style="width:54pt" align="center">語文</td>
<td style="width:54pt" align="center">數學</td>
<td style="width:54pt" align="center">英語</td>
</tr>
<tr>
<td align="center">1</td>
<td style="background:#C6EFCE" align="center">張三</td>
<td style="background:#BDD7EE" align="center">90</td>
<td style="background:#C6EFCE" align="center">85</td>
<td style="background:#BDD7EE" align="center">100</td>
</tr>
<tr>
<td align="center">2</td>
<td style="background:#C6EFCE" align="center">李四</td>
<td style="background:#BDD7EE" align="center">99</td>
<td style="background:#C6EFCE" align="center">85</td>
<td style="background:#BDD7EE" align="center">80</td>
</tr>
</table>
</body>
</html>
background-color / background:單元格背景color:字體顏色font-weight: bold:加粗text-align:對齊border:邊框width/height:列寬/行高mso-number-format:數字格式(如 @ 文本、0.00 兩位小數)<td colspan="3">跨三列</td> <td rowspan="2">跨兩行</td>
# PEAR 安裝 pear install OLE pear install Spreadsheet_Excel_Writer # 或 Composer (非官方分支) composer require phpoffice/spreadsheet-excel-writer
require_once 'Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer();
$filename = date('YmdHis') . '.xls';
$workbook->send($filename); // 發送下載頭
$workbook->setVersion(8); // Excel 97+ 格式
$sheet = $workbook->addWorksheet('成績單');
$sheet->setInputEncoding('utf-8');
// 定義格式
$titleFmt = $workbook->addFormat([
'Size' => 14, 'Align' => 'center', 'Bold' => 1,
'Color' => 'white', 'FgColor' => 'navy', 'Border' => 1
]);
$headerFmt = $workbook->addFormat([
'Size' => 12, 'Align' => 'center', 'Bold' => 1,
'FgColor' => 'silver', 'Border' => 1
]);
$dataFmt = $workbook->addFormat([
'Size' => 10, 'Align' => 'center', 'Border' => 1
]);
// 列寬
$sheet->setColumn(0, 4, 15);
// 寫入標題(合並單元格)
$sheet->write(0, 0, '成績單', $titleFmt);
$sheet->mergeCells(0, 0, 0, 4);
// 表頭
$headers = ['編號', '姓名', '語文', '數學', '英語'];
foreach ($headers as $col => $h) {
$sheet->write(1, $col, $h, $headerFmt);
}
// 數據
$data = [
[1, '張三', 90, 85, 100],
[2, '李四', 99, 85, 80],
];
foreach ($data as $rowIdx => $row) {
foreach ($row as $colIdx => $val) {
$sheet->write($rowIdx + 2, $colIdx, $val, $dataFmt);
}
}
$workbook->close();
| 維度 | HTML 表格法 | Spreadsheet_Excel_Writer |
|---|---|---|
| 依賴 | 無 | PEAR/Composer 包 |
| 格式 | HTML 偽裝 .xls | 真正 BIFF .xls 二進制 |
| 樣式豐富度 | 基本 CSS 子集 | 完整 Excel 格式支持 |
| 大數據性能 | 較差(字符串拼接) | 較好(流式寫入) |
| 兼容性 | Excel/WPS/LibreOffice 皆可 | Excel 完美,其他可能有差異 |
| 維護狀態 | 長期可用 | PEAR 庫已停止更新 |
composer require phpoffice/phpspreadsheet
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello')->getStyle('A1')->getFont()->setBold(true)->setSize(14);
$writer = new Xlsx($spreadsheet);
$writer->save('php://output'); // 直接輸出下載