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

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

 

转载于:https://my.oschina.net/u/3268486/blog/884358

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值