1. 去 http://phpexcel.codeplex.com/ 下载phpexcel源码,解压缩后把phpexcel/Classes里的PHPExcel文件夹和PHPExcel.php复制到CI的application/libraries文件夹内。目录结构如下:
---application/libraries/PHPExcel.php
---application/libraries/PHPExcel(文件夹)
2. 修改application/libraries/PHPExcel/IOFactory.php文件:将其类名从PHPExcel_IOFactory改为IOFactory,遵从CI类命名规则;将其构造函数改为public
3. 在CI内phpexcel的使用:
//加载PHPExcel的类
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
//创建PHPExcel实例
$excel = new PHPExcel();
//下面介绍项目中用到的几个关于excel的操作
//为单元格赋值
$excel->getActiveSheet()->setCellValue('A1','aaa');
//合并单元格
$excel->getActiveSheet()->mergeCells('A1:A2');
//设置单元格内文字垂直居中
$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//设置单元格内文字自动换行
$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
//为单元格添加注释
$excel->getActiveSheet()->getComment('A1')->getText()->createTextRun('hello');
//设置单元格文字颜色
$excel->getActiveSheet()->getStyle('A1')->getFont()->getColor->setARGB(PHPExcel_Style_Color::COLOR_RED);
//输出到浏览器
$write = new PHPExcel_Writer_Excel2007($excel);
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="test.xlsx"');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');