在脚本学堂之前的文章中介绍了使用phpexcel中导出数据到Excel文件的方法,可以参考:
后来发现一个针对Codeigniter的类:CI-Excel-Generation-Library,使用方法十分简单。
1、下载CI-Excel-Generation-Library
地址:https://github.com/JOakley77/CI-Excel-Generation-Library
2、将Excel.php放到libraries中。
3、使用方法:
从数据库生成excel:
 
<?php
public function export() {
 $this->load->library('table');
 $this->load->library('excel');
 $sql = $this->db->get('dbtable');
 $query->result();
 $this->excel->filename = 'abc123';
 $this->excel->make_from_db($sql);
}
?>
从数组生成excel:
 
<?php
public function export() {
 $titles = array('field1', 'field2', 'field3');
 $array = array();
 for ($i = 0; $i <= 100; $i++) {
  $array[] = array($i, $i+1, $i+2);
 }
 $this->excel->make_from_array($titles, $array);
}
?>