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');