【例子介绍】网页导出excel,支持中文
【相关图片】
【源码结构】
<?php include 'PHPExcel.php'; $letter = array('A','B','C','D','E','F','F','G');//Excel表格式,这里简略写了8列 $tableheader = array('学号','姓名','性别','年龄','班级');//表头数组 $data = array( array('1','小王','男','20','100'), array('2','小李','男','20','101'), array('3','小张','女','20','102'), array('4','小赵','女','20','103') ); excel_save($letter,$data,$tableheader); function excel_save($letter,$data,$tableheader){//处理函数 $excel = new PHPExcel(); //填充表头信息 for($i = 0;$i < count($tableheader);$i ) { $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]"); } //填充表格信息 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 ; } } //创建Excel输入对象 $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="报表.xls"'); header("Content-Transfer-Encoding:binary"); $write->save('php://output'); } ?>
评论