在 PHP 開發中,資料匯出與二維碼生成是常見需求。本文整理 PHPExcel(現已併入 PhpSpreadsheet)與 phpqrcode 的核心參數與實戰範例,幫助開發者快速上手。
<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/Writer/Excel2007.php'; // .xlsx 格式
// require_once 'PHPExcel/Writer/Excel5.php'; // .xls 格式
$objPHPExcel = new PHPExcel();
儲存至伺服器:
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save("report.xlsx");
直接輸出下載:
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="report.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
$props = $objPHPExcel->getProperties();
$props->setCreator("Author Name");
$props->setLastModifiedBy("Author Name");
$props->setTitle("Report Title");
$props->setSubject("Report Subject");
$props->setDescription("Description");
$props->setKeywords("keyword1, keyword2");
$props->setCategory("Category");
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setTitle('DataSheet');
// 設定儲存格值
$sheet->setCellValue('A1', 'String');
$sheet->setCellValue('A2', 123);
$sheet->setCellValue('A3', true);
$sheet->setCellValue('C5', '=SUM(C2:C4)'); // 公式
// 合併/拆分儲存格
$sheet->mergeCells('A1:E1');
$sheet->unmergeCells('A3:B3');
$style = $sheet->getStyle('A1:E1');
// 字型
$style->getFont()->setName('Microsoft JhengHei');
$style->getFont()->setSize(14);
$style->getFont()->setBold(true);
$style->getFont()->getColor()->setARGB('FFFFFFFF');
// 對齊
$style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$style->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 邊框
$style->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
// 填滿顏色
$style->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$style->getFill()->getStartColor()->setARGB('FF4472C4');
$sheet->getColumnDimension('A')->setAutoSize(true);
$sheet->getColumnDimension('B')->setWidth(20);
$drawing = new PHPExcel_Worksheet_Drawing();
$drawing->setName('Logo');
$drawing->setPath('./images/logo.png');
$drawing->setHeight(50);
$drawing->setWorksheet($sheet);
function toUtf8($str) {
return empty($str) ? '' : iconv('GBK', 'UTF-8', $str);
}
// 假設 $rows 為從資料庫查詢的二維陣列
foreach ($rows as $i => $row) {
$line = $i + 2; // 第 1 列為標題
$sheet->setCellValue('A' . $line, toUtf8($row['name']));
$sheet->setCellValue('B' . $line, toUtf8($row['email']));
// ... 其他欄位
}
$objPHPExcel->createSheet(); // 新增第二個工作表
$objPHPExcel->setActiveSheetIndex(1);
$sheet2 = $objPHPExcel->getActiveSheet();
$sheet2->setTitle('Sheet2');
phpqrcode 是輕量級 QR Code 生成庫,核心函式 QRcode::png() 參數如下:
| 參數 | 說明 | 預設值 |
|---|---|---|
$text | 編碼內容(字串) | 必填 |
$outfile | 輸出檔案路徑;false 直接輸出瀏覽器 | false |
$level | 容錯等級:L(7%)、M(15%)、Q(25%)、H(30%) | L |
$size | 單模組像素大小 | 3 |
$margin | 靜區寬度(模組數) | 4 |
$saveandprint | 是否儲存並顯示 | false |
require_once 'phpqrcode/qrlib.php';
// 直接輸出
QRcode::png('https://example.com', false, 'M', 5, 2);
// 儲存檔案
QRcode::png('WiFi:T:WPA;S:MySSID;P:password;;', 'wifi.png', 'H', 8, 4);
PhpSpreadsheet 的 CellCaching 機制。iconv 轉碼三者一致(皆為 UTF-8)。$writer->setPreCalculateFormulas(true)。