2、使用pear的Spreadsheet_Excel_Writer类
下载地址: http://pear.php.net/package/Spreadsheet_Excel_Writer
此类依赖于OLE,下载地址:http://pear.php.net/package/OLE
需要注意的是导出的Excel文件格式比较老,修改后保存会提示是否转换成更新的格式。
不过可以设定格式,很强大。
PHP代码
<?php
require_once ‘Spreadsheet/Excel/Writer.php’;
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
// sending HTTP headers
$workbook->send(’test.xls’);
// Creating a worksheet
$worksheet =& $workbook->addWorksheet(’My first worksheet’);
// The actual data
$worksheet->write(0, 0, ‘Name’);
$worksheet->write(0, 1, ‘Age’);
$worksheet->write(1, 0, ‘John Smith’);
$worksheet->write(1, 1, 30);
$worksheet->write(2, 0, ‘Johann Schmidt’);
$worksheet->write(2, 1, 31);
$worksheet->write(3, 0, ‘Juan Herrera’);
$worksheet->write(3, 1, 32);
// Let’s send the file
$workbook->close();
?>
3:利用smarty,生成符合Excel规范的XML或HTML文件
支持格式,非常完美的导出方案。不过导出来的的本质上还是XML文件,如果用来导入就需要另外处理了。
详细内容请见rardge大侠的帖子:http://bbs.chinaunix.net/viewthread.php?tid=745757
需要注意的是如果导出的表格行数不确定时,最好在模板中把”ss:ExpandedColumnCount=”5″ ss:ExpandedRowCount=”21″”之类的东西删掉。
4、利用pack函数打印出模拟Excel格式的断句符号,这种更接近于Excel标准格式,用office2003修改后保存,还不会弹出提示,推荐用这种方法。
缺点是无格式。
PHP代码
<?php
// Send Header
header(”Pragma: public”);
header(”Expires: 0″);
header(”Cache-Control: must-revalidate, post-check=0, pre-check=0″);
header(”Content-Type: application/force-download”);
header(”Content-Type: application/octet-stream”);
header(”Content-Type: application/download”);;
header(”Content-Disposition: attachment;filename=test.xls “);
header(”Content-Transfer-Encoding: binary “);
// XLS Data Cell
xlsBOF();
xlsWriteLabel(1,0,”My excel line one”);
xlsWriteLabel(2,0,”My excel line two : “);
xlsWriteLabel(2,1,”Hello everybody”);
xlsEOF();
function xlsBOF() {
echo pack(”ssssss”, 0×809, 0×8, 0×0, 0×10, 0×0, 0×0);
return;
}
function xlsEOF() {
echo pack(”ss”, 0×0A, 0×00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack(”sssss”, 0×203, 14, $Row, $Col, 0×0);
echo pack(”d”, $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack(”ssssss”, 0×204, 8 + $L, $Row, $Col, 0×0, $L);
echo $Value;
return;
}
?>
不过笔者在64位linux系统中使用时失败了,断句符号全部变成了乱码。
5、使用制表符、换行符的方法
制表符”\t”用户分割同一行中的列,换行符”\t\n”可以开启下一行。
<?php
header(”Content-Type: application/vnd.ms-execl”);
header(”Content-Disposition: attachment; filename=myExcel.xls”);
header(”Pragma: no-cache”);
header(”Expires: 0″);
/*first line*/
echo “hello”.”\t”;
echo “world”.”\t”;
echo “\t\n”;
/*start of second line*/
echo “this is second line”.”\t”;
echo “Hi,pretty girl”.”\t”;
echo “\t\n”;
?>
(责任编辑:ken)