<?php
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$Value = iconv("UTF-8", "gb2312", $Value); //加上本语句,解决导出excel文件乱码问题20110629
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
// 文件头
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-Type: application/vnd.ms-excel; charset=UTF-8");
//header("Content-Disposition: attachment;filename=ExcelName.xls ");
header("Content-Disposition: inline; filename=ExcelName.xls");
header("Content-Transfer-Encoding: binary ");
// 向表中添加数据
xlsBOF();
xlsWriteLabel(0,0,"编号");
xlsWriteLabel(0,1,"姓名");
xlsWriteLabel(0,2,"年龄");
xlsWriteLabel(0,3,"电话");
xlsWriteLabel(0,4,"邮编");
$xlsRow = 1;
for($i=0;$i<10;$i++) {
xlsWriteNumber($xlsRow,0,"$i");
xlsWriteLabel($xlsRow,1,"金龙$i");
xlsWriteLabel($xlsRow,2,"1$i");
xlsWriteLabel($xlsRow,3,"1599955996$i");
xlsWriteLabel($xlsRow,4,"51800$i");
$xlsRow++;
}
xlsEOF();
exit();
?>
附加:推荐PHPExcel,这是比较强大的Excel操作,导入导出都可以,大家可以去看看。