ci框架整合phpexcel,實現數據導出excel

1.搜索「phpexcel」,下載php

2.解壓後如圖瀏覽器

3.打開Class文件夾,全選複製app

4.找到項目的  libraries 文件夾粘貼,不是  sysm\libraries這個文件夾,若是沒有就新建一個,post

5.打開 phpexcel.php,加入一行代碼ui

require(PHPEXCEL_ROOT . 'PHPExcel/IOFactory.php');

6.打開 PHPExcel 文件夾,打開this

IOFactory.php

PHPExcel_IOFactory 修改成  IOFactoryspa

//class PHPExcel_IOFactory
class IOFactory

7.程序中引用日誌

引入excel

$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');

建立對象對象

$excel = new PHPExcel();
//列名
$excel->getActiveSheet()->setCellValue('A1', '登陸時間');
$excel->getActiveSheet()->setCellValue('B1', '登陸ip');

遍歷查詢結果,

foreach ($logsInfo as $logsKey => $logsVal){
                    $num = $logsKey+2;
                    $excel->getActiveSheet()->setCellValue('A'.$num, date('Y-m-d',$logsVal['time']));
                    $excel->getActiveSheet()->setCellValue('C'.$num, $logsVal['IP']);
                }

輸出到瀏覽器,並下載到本地

                $write = new PHPExcel_Writer_Excel2007($excel);
                $excelName  = $username.'-登錄日誌-'.date('Y-m-d').'.xlsx';
                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=".$excelName);
                header("Content-Transfer-Encoding:binary");
                $write->save('php://output');
相關文章
相關標籤/搜索