上面的答案在PHPExcel会给你很好的结果。但是如果你想用corePHP来实现它,那么这里就是代码来暗示。
<?php
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;
}
// prepare headers information
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=\"export_".date("Y-m-d").".xls\"");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
// start exporting
xlsBOF();
// first row
xlsWriteLabel(0, 0, "id");
xlsWriteLabel(0, 1, "name");
xlsWriteLabel(0, 2, "email");
// second row
xlsWriteNumber(1, 0, 230);
xlsWriteLabel(1, 1, "John");
xlsWriteLabel(1, 2, "[email protected]");
// third row
xlsWriteNumber(2, 0, 350);
xlsWriteLabel(2, 1, "Mark");
xlsWriteLabel(2, 2, "[email protected]");
// end exporting
xlsEOF();
exit;
?>
参考从http://krasimirtsonev.com/blog/article/php-export-mysql-data-to-xls-file
您是否尝试设置文件名以'MyFile.xlsx'? – 2012-08-03 09:35:57
我认为当您试图在Excel中打开文件时出现错误?那么,正如你已经注意到你得到这个错误,因为你没有在文件中的任何内容,它显然不会打开,直到你输出一个实际的Excel xls文件。 当你开始输出实际的Excel内容时,它应该消失。该警告让您知道格式实际上不是Excel文件。当你开始使用PHPExcel时,它将会/应该消失。 – 2012-08-03 09:38:22