在 PHP 專案中,若需頻繁匯出 Excel 檔案,將匯出邏輯封裝為可重用的類別能大幅提升開發效率。本文介紹一個基於 PHPExcel 的 Excel 導出類,支援自訂檔名、格式(Excel 2007 / Excel 5)、欄位過濾、排序、全域樣式、標題合併、欄位樣式、條件式合併儲存格等豐富功能,並提供完整的程式碼與使用範例。
.xlsx (2007) 與 .xls (Excel5) 兩種格式delKey)<?php
require_once 'excelC.class.php';
$excel = new excelC();
$excel->fileName = '學科列表'; // 設定檔名
$excel->format = '2007'; // 設定格式:2007 為 .xlsx,其他為 .xls
$record = array(
'delKey' => array('id', 'addTime', 'status'), // 不匯出的欄位
'sort' => array(
'keyName' => array('subjectName', 'flag'), // 排序欄位
'reorder' => 'DESC' // DESC 倒序,ASC 正序
),
'excelStyle' => array(
'setName' => 'Arial', // 全域字體
'setSize' => '12' // 全域字體大小
),
'title' => array(
'tableName' => '學科列表',
'center' => true,
'direction' => 'right', // 合併方向:right, left, up, down
'merge' => '2', // 合併幾個儲存格
'setSize' => '30' // 標題字體大小
),
'data' => array(
array('tableName' => '科目名稱', 'width' => '30', 'setName' => '宋體', 'setSize' => '20', 'background' => 'red', 'textColor' => 'white', 'bold' => true, 'underline' => true, 'borderColor' => 'cyan', 'center' => true),
array('tableName' => '學科', 'width' => '50', 'center' => true)
),
'merge' => array(
'all' => array('width' => '30', 'setName' => '宋體', 'setSize' => '20', 'background' => 'red', 'textColor' => 'white', 'bold' => true, 'underline' => true, 'borderColor' => 'cyan', 'center' => true)
)
);
// $data 為從資料庫取得的資料陣列或物件
$excel->export($record, $data);
?>
以下為 excelC 類別完整實作,包含屬性設定、樣式處理、資料處理、輸出下載等私有與公有方法。
<?php
$address = dirname(dirname(__FILE__)).'/PHPExcel';
include $address.'/PHPExcel.class.php';
include $address.'/PHPExcel/Writer/Excel2007.php';
include $address.'/PHPExcel/Writer/Excel5.php';
include $address.'/PHPExcel/IOFactory.php';
class excelC {
public $format = '2007';
public $fileName;
private $objExcel;
private $letters;
public function __construct() {
$this->fileName = time();
$this->fileTitle = '導出數據';
$this->objExcel = new PHPExcel();
$this->letters = $this->letter();
}
private function attribute() { /* 設定文件屬性 */ }
private function sheet() { /* 設定工作表 */ }
public function export($record=array(), $data=array()) { /* 主導出流程 */ }
private function excelData(&$record, &$data) { /* 協調各樣式與資料 */ }
private function whole(&$record) { /* 全域樣式 */ }
private function excelTitle($record, $num) { /* 標題列 */ }
private function tableHeader($record) { /* 表頭 */ }
private function tableContent(&$record, &$data) { /* 表格內容 */ }
private function Appoint($kk, $vv, $letters, $hang, $merge) { /* 指定儲存格樣式 */ }
private function appOintStyle($letters, $hang, $v) { /* 套用綜合樣式 */ }
private function mergeCells($letters, $hang, $direction, $merge) { /* 合併儲存格 */ }
private function setVertical($letter, $data) { /* 垂直置中 */ }
private function backColor($color) { /* 顏色對照表 */ }
private function maniData($record, $data) { /* 資料預處理:刪除欄位、排序 */ }
private function objectToArray($data) { /* 物件轉陣列 */ }
private function delSort($data, $delKey='', $sort='') { /* 刪除與排序 */ }
private function delData($data, &$delKey) { /* 刪除指定鍵值 */ }
private function sortData($data, &$sort) { /* 排序邏輯 */ }
private function down() { /* 輸出下載 */ }
public function letter() { /* 產生欄位字母陣列 */ }
}
?>
若環境為 PHP 5.1 等舊版本,無法運行 PHPExcel,可改用原生 HTTP 標頭輸出 HTML 表格,瀏覽器會自動下載為 .xls:
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=export.xls");
echo '<table><tr><td>內容</td></tr></table>';
常見原因包括編碼不一致、記憶體不足、或 PHPExcel 版本相容性問題。建議:
ini_set('memory_limit', '256M');PHPExcel_IOFactory::createWriter($obj, 'Excel5') 產生舊版格式這個封裝類別將複雜的 Excel 匯出細節隱藏,開發者只需準備資料與設定陣列即可快速產出格式豐富的試算表。若專案需求更進階(如大數據分批匯出、圖表、公式),可考慮改用 PhpSpreadsheet(PHPExcel 繼任者)或搭配 CSV 分批輸出。