以下是我php輸出excel的腳本
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
}
function xlsWriteLabel($Row, $Col, $Value) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
}
ob_end_clean();
ob_clean();
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header("Content-Transfer-Encoding: binary");
$filename = urlencode('export_'.date("Y-m-d-His").'.xls');
header("Content-Disposition: attachment; filename='.$filename.'");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
header("Pragma: public");
xlsBOF();
xlsWriteLabel(0, 0, "中文好");
xlsWriteLabel(0, 1, "中文妙");
xlsWriteLabel(0, 2, "中文第一");
$order = $pdo->query(
"SELECT * FROM `order_record` as r "
);
$i = 1;
while ($row = mysqli_fetch_array($order)) {
xlsWriteNumber($i, 0, 230);
xlsWriteLabel($i, 1, "John");
xlsWriteLabel($i, 2, "john@yahoo.com");
$i++;
}
xlsEOF();
會出現這樣的中文亂碼
https://imgur.com/5OoY3mo
稍微查了一下好像跟GBA有關係,
只是不管怎麼用 iconv 轉換都還是一樣,內容都會有亂碼
甚至開啟檔案之前會出現這個錯誤....
https://imgur.com/FoB5Fb2
或是 php 還有什麼其他簡單的方式能夠輸出 excel 不會產生亂碼問題呢?