Codeigniter+PHPExcel导出数据到Excel文件

解压压缩包里的Classes文件夹中的内容到application\libraries\目录下,目录结构如下:
--application\libraries\PHPExcel.php
--application\libraries\PHPExcel(文件夹)
修改application\libraries\PHPExcel\IOFactory.php文件
--将其类名从PHPExcel_IOFactory改为IOFactory,遵从CI类命名规则。
--将其构造函数改为public

 1 $this->load->library('PHPExcel');
 2         $this->load->library('PHPExcel/IOFactory');
 3         $resultPHPExcel = new PHPExcel();
 4         $resultPHPExcel->getActiveSheet()->setCellValue('A1', '项目'); 
 5         $resultPHPExcel->getActiveSheet()->setCellValue('B1', '结果'); 
 6         $resultPHPExcel->getActiveSheet()->setCellValue('C1', '数量'); 
 7         $i = 2; 
 8         foreach($m_data['jc_fia'] as $key){ 
 9             $resultPHPExcel->getActiveSheet()->setCellValue('A' . $i, $key[0]); 
10             $resultPHPExcel->getActiveSheet()->setCellValue('B' . $i, $key[1]); 
11             $resultPHPExcel->getActiveSheet()->setCellValue('C' . $i, $key[0]); 
12             $i ++; 
13         }
14         $outputFileName = "123.xls";
15         $xlsWriter = new PHPExcel_Writer_Excel5($resultPHPExcel); 
16         header("Content-Type: application/force-download"); 
17         header("Content-Type: application/octet-stream"); 
18         header("Content-Type: application/download"); 
19         header('Content-Disposition:inline;filename="'.$outputFileName.'"'); 
20         header("Content-Transfer-Encoding: binary"); 
21         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
22         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
23         header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
24         header("Pragma: no-cache"); 
25         $xlsWriter->save( "php://output" );

 

posted @ 2016-04-15 14:16  YGCool  阅读(2163)  评论(0编辑  收藏  举报