Excel导出
Posted mfblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Excel导出相关的知识,希望对你有一定的参考价值。
1 //设置header头文件 2 header("content-type:text/html;charset=utf-8"); 3 //设置文件大小的限制 4 ini_set("memory_limit",‘1024M‘); 5 //引入类文件 6 Loader::import("phpExcel.Classes.PHPExcel"); 7 //实例化文文件 8 $obj = new \PHPExcel(); 9 //打开excel,获取当前展示出来的页面 10 $sheet = $obj->getActiveSheet(); 11 12 //查询数据库 13 $data = Db::table("day7")->select(); 14 //赋值表格(sheet重命名标题) 15 $sheet->setTitle("测试导出"); 16 //给单元格赋值 17 $sheet->setCellValue("A1","ID") 18 ->setCellValue("B1","导航名称") 19 ->setCellValue("C1","导航链接"); 20 21 //开始位置从第二行开始 22 $j = 2; 23 foreach($data as $k=>$v){ 24 $sheet->setCellValue("A".$j,$v[‘id‘]) 25 ->setCellValue("B".$j,$v[‘file_name‘]) 26 ->setCellValue("C".$j,$v[‘path‘]); 27 $j++; 28 } 29 //导出 30 header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘); 31 header(‘Content-Disposition: attachment;filename="01simple.xlsx"‘); 32 header(‘Cache-Control: max-age=0‘); 33 // If you‘re serving to IE 9, then the following may be needed 34 header(‘Cache-Control: max-age=1‘); 35 36 // If you‘re serving to IE over SSL, then the following may be needed 37 header (‘Expires: Mon, 26 Jul 1997 05:00:00 GMT‘); // Date in the past 38 header (‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s‘).‘ GMT‘); // always modified 39 header (‘Cache-Control: cache, must-revalidate‘); // HTTP/1.1 40 header (‘Pragma: public‘); // HTTP/1.0 41 42 $objWriter = \PHPExcel_IOFactory::createWriter($obj, ‘Excel2007‘); 43 $objWriter->save(‘php://output‘);
以上是关于Excel导出的主要内容,如果未能解决你的问题,请参考以下文章