1.下载phpexcel
2.在页面引入phpexcel的类文件,并且造该类的对象
include("../chajian/phpexcel/Classes/PHPExcel.php"); $excel = new PHPExcel();
3.查询数据库得出二维数组这里数组名为:$attr
4.导出excel代码,表头数组和数据数组对应:
//Excel表格式,这里简略写了10列 $letter = array(‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘F‘,‘G‘,‘H‘,‘I‘); //表头数组 $tableheader = array(‘汇报时间‘,‘汇报完成度‘,‘汇报人员‘,‘最终完成度‘,‘导师评语‘); //填充表头信息 for($i = 0;$i < count($tableheader);$i++) { $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]"); } //填充表格信息 for ($i = 2;$i <= count($attr) + 1;$i++) { $j = 0; foreach ($attr[$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=‘{$sj}_{$bt}作业.xls‘"); header("Content-Transfer-Encoding:binary"); $write->save(‘php://output‘);
5.如果长数字串显示为科学计数解决方法
(1)在设置值的时候显示的指定数据类型
$excel->getActiveSheet()->setCellValueExplicit(‘D1‘,120000000000,PHPExcel_Cell_DataType::TYPE_STRING);
(2)在数字字符串前加一个空格使之成为字符串
$excel->getActiveSheet()->setCellValue(‘D1‘,‘ ‘ . 120000000000);
时间: 2024-10-11 01:38:57