若覺得 PHPExcel 過於龐大,CI-Excel-Generation-Library 提供極簡介面,幾行程式碼即可從資料庫或陣列產生 Excel 檔案。適合輕量級匯出需求。
Excel.php 放入 application/libraries/ 目錄。<?php
public function export() {
$this->load->library('table');
$this->load->library('excel');
$sql = $this->db->get('dbtable');
// 注意:原範例有誤,應為 $query = $this->db->get('dbtable');
$query = $this->db->get('dbtable');
$this->excel->filename = 'abc123';
$this->excel->make_from_db($query);
}
?>
<?php
public function export() {
$this->load->library('excel');
$titles = array('field1', 'field2', 'field3');
$array = array();
for ($i = 0; $i <= 100; $i++) {
$array[] = array($i, $i+1, $i+2);
}
$this->excel->make_from_array($titles, $array);
}
?>
| 特性 | CI-Excel-Generation-Library | PHPExcel / PhpSpreadsheet |
|---|---|---|
| 程式碼複雜度 | 極低 | 中高 |
| 功能豐富度 | 基本匯出 | 完整讀寫、樣式、圖表、公式 |
| 記憶體佔用 | 較低 | 較高 |
| 格式支援 | .xls (BIFF) | .xlsx, .xls, .ods, .csv 等 |
| 維護狀態 | 社群較小 | PhpSpreadsheet 積極維護中 |