您的位置:首页 > 编程语言 > PHP开发

ci框架整合phpexcel,实现数据导出excel

2017-04-21 00:00 741 查看
1.搜索“phpexcel”,下载

2.解压后如图



3.打开Class文件夹,全选复制

4.找到项目的 libraries 文件夹粘贴,不是 sysm\libraries这个文件夹,如果没有就新建一个,

5.打开 phpexcel.php,加入一行代码

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

6.打开 PHPExcel 文件夹,打开

IOFactory.php

将 PHPExcel_IOFactory 修改为 IOFactory

//class PHPExcel_IOFactory
class IOFactory

7.程序中引用

引入

$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');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ci框架 phpexcel