php导出excel表格⽅式⼀:使⽤PHPExcel类库
//引⼊PHPExcel库⽂件(路径根据⾃⼰情况)
include './phpexcel/Classes/PHPExcel.php';
$excel = new PHPExcel(); //创建对象
$letter = array('A','B','C','D','E','F','F','G'); //Excel表格式,这⾥简略写了8列$tableheader = array('学号','姓名','性别','年龄','班级');//表头数组
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
$data = array(
array('1','⼩王','男','20','100'),
array('2','⼩李','男','20','101'),
array('3','⼩张','⼥','20','102'),
array('4','⼩赵','⼥','20','103')
);
//填充表格信息
for ($i = 2;$i <= count($data) + 1;$i++) {
$j = 0;
foreach ($data[$i - 2] as $key=>$value) {
$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
$j++;
}
}
$write = new PHPExcel_Writer_Excel5($excel);
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/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="testdata.xls"');
header("Content-Transfer-Encoding:binary");
php中header是什么意思
$write->save('php://output');
⽅式⼆:简单的PHP导出excel,不适⽤任何外部类库⽂件
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=reply.csv");
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen("php://output", "w");
$header = array('学号','姓名','性别','年龄','班级');
$converter = function($value) {
return iconv('utf-8', 'gbk', $value);
};
$header = array_map($converter, $header);
$list = array(
array('1','⼩王','男','20','100'),
array('2','⼩李','男','20','101'),
array('3','⼩张','⼥','20','102'),
array('4','⼩赵','⼥','20','103')
);
fputcsv($output, $header);
foreach($list as $k => $v)
{
$csvrow = array_map($converter, array(
$v[0],
$v[1],
$v[2],
$v[3],
$v[4],
));
fputcsv($output, $csvrow); }
fclose($output);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论