1、下载php
phpexcel:http://phpexcel.codeplex.com/数组
2、浏览器
把classes下的文件copy到ci框架下的application/libraries目录下。app
能够先看看example目录的例子。框架
3、简单的封装ui
放在application/libraries目录下this
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** Include PHPExcel */ require_once dirname(__FILE__) . '/PHPExcel.php'; /** * Class TableExport */ class TableExport { /** @var PHPExcel */ private $_PHPExcel; /** * * @param PHPExcel $arg_phpExcel */ public function __construct($arg_phpExcel = null) { if (is_null($arg_phpExcel) || !($arg_phpExcel instanceof PHPExcel)) $this->_PHPExcel = new PHPExcel(); else $this->_PHPExcel = $arg_phpExcel; } /** * * @return PHPExcel_DocumentProperties */ public function getProperties() { return $this->_PHPExcel->getProperties(); } /** * 编写内容 * @notice 严格注意传入的数组格式.这里会自动忽略一些异常的行数据的 * @param array $arg_title 一维数组 表格列名标题 * @param array $arg_comment 二维数组 * @todo:未中文编码处理 * @throws PHPExcel_Exception * @return $this */ public function setComment(array $arg_title, array $arg_comment) { static $AChar = 65; $column = 0; if (empty($arg_title) || !is_array($arg_title) || !is_array($arg_comment)) { throw new PHPExcel_Exception("TableExport::setComment arg_title is empty or the args not is array"); } $column_count = count($arg_title); $this->_PHPExcel->setActiveSheetIndex(0); //设置列名 foreach ($arg_title as $val) { $this->_PHPExcel->getActiveSheet()->getStyle(chr($AChar + $column) . '1')->getFont()->setName('Candara'); $this->_PHPExcel->getActiveSheet()->getStyle(chr($AChar + $column) . '1')->getFont()->setSize(16); $this->_PHPExcel->getActiveSheet()->getStyle(chr($AChar + $column) . '1')->getFont()->setBold(true); $this->_PHPExcel->getActiveSheet()->getColumnDimension(chr($AChar + $column))->setWidth(21); $this->_PHPExcel->getActiveSheet()->setCellValue(chr($AChar + $column) . '1', $val); $column++; } //填写内容 $column = 0; //列 $row = 2; //行 foreach ($arg_comment as $rows) { //若是行内容异常,则忽略本行 if (!is_array($rows) || count($rows) != $column_count) continue; $column = 0; //列 foreach ($rows as $val) { $this->_PHPExcel->getActiveSheet()->setCellValue(chr($AChar + $column) . $row, $val); $column++; } $row++; } $this->_PHPExcel->setActiveSheetIndex(0); return $this; } /** * 输出内容到网页,提供下载 * * @param string $arg_filename 下载文件名 */ public function outputDownload_Excel5($arg_filename) { // 重定向输出到浏览器客户端 (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $arg_filename . '"'); header('Cache-Control: max-age=0'); // IE 9下的头部信息 header('Cache-Control: max-age=1'); // IE SSL header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $objWriter = PHPExcel_IOFactory::createWriter($this->_PHPExcel, 'Excel5'); $objWriter->save('php://output'); } public function convertUTF8($str) { if (empty($str)) return ''; return iconv('gb2312', 'utf-8', $str); } /** * 魔术方法__call * * @param $method * @param $args * @return mixed|void */ public function __call($method, $args) { //调用PHPExcel本身的方法 if (method_exists($this->_PHPExcel, $method)) { return call_user_func_array(array($this->_PHPExcel, $method), $args); } else { //TODO:异常处理 return false; } } }
上面主要有两个方法,编码
setComment()输入excel内容,首先是列名,占据了第一行位置(A1,B1,C1....),而后真正的数据从第二行开始。 outputDownload_Excel5()输出Excel5格式的文件流到浏览器
其余方法都所有委托到本来的PHPExcel中去。excel
而后在控制器下调用code
$this->load->library('TableExport'); $this->tableexport->getProperties()->setTitle("xxxx")->setSubject("xxxx"); $this->tableexport->getDefaultStyle()->getFont()->setSize(13); $this->tableexport->setComment($column, $rows)->outputDownload_Excel5("xxx.xls");
而后浏览器下会提示查看或者保存的(谷歌下直接下载了)。
4、结束语
望指正!