Codeigniter+PHPExcel中导出数据到Excel文件
2012-03-27 18:11
369 查看
1. 准备工作
下载PHPExcel:http://phpexcel.codeplex.com这是个强大的Excel库,这里只演示导出Excel文件的功能,其中的大部分功能可能都用不着。
2. 安装PHPExcel到Codeigniter
1) 解压压缩包里的Classes文件夹中的内容到application\libraries\目录下,目录结构如下:-- application\libraries\PHPExcel.php
-- application\libraries\PHPExcel (文件夹)
2)修改application\libraries\PHPExcel\IOFactory.php 文件
-- 将其类名从PHPExcel_IOFactory改为IOFactory,遵从CI类命名规则。
-- 将其构造函数改为public
3. 安装完毕,写一个导出excel的控制器(Controller)
代码如下:<?php class Table_export extends CI_Controller { function __construct() { parent::__construct(); // Here you should add some sort of user validation // to prevent strangers from pulling your table data } function index($table_name) { $query = $this->db->get($table_name); if(!$query) return false; // Starting the PHPExcel library $this->load->library('PHPExcel'); $this->load->library('PHPExcel/IOFactory'); $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setTitle("export")->setDescription("none"); $objPHPExcel->setActiveSheetIndex(0); // Field names in the first row $fields = $query->list_fields(); $col = 0; foreach ($fields as $field) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field); $col++; } // Fetching the table data $row = 2; foreach($query->result() as $data) { $col = 0; foreach ($fields as $field) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field); $col++; } $row++; } $objPHPExcel->setActiveSheetIndex(0); $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); // Sending headers to force the user to download the file header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"'); header('Cache-Control: max-age=0'); $objWriter->save('php://output'); } }
4. 测试
加入数据库有表名为products,此时可以访问http://www.yoursite.com/table_export/index/products 导出Excel文件了。参考:http://www.dannyherran.com/2011/03/exporting-your-mysql-table-data-with-phpexcel-codeigniter/
相关文章推荐
- Codeigniter+PHPExcel中导出数据到Excel文件
- CodeIgniterCodeigniter+PHPExcel导出数据到Excel文件
- Codeigniter+PHPExcel实现导出数据到Excel文件
- CodeIgniter+PHPExcel 导出数据为Excel文件
- Codeigniter+PHPExcel中导出数据到Excel文件//http://www.cnblogs.com/jiji262/archive/2012/03/27/2419989.html
- 利用PHPExcel将数据导出到xls格式的excel文件
- 利用PHPExcel将数据导出到xls格式的excel文件
- Codeigniter+PHPExcel导出Excel文件
- 利用PHPExcel将数据导出到xls格式的excel文件
- Codeigniter+PHPExcel实现导出数据到Excel文件
- Codeigniter+PHPExcel中导出数据到Excel文件
- 使用PHPExcel导出数据
- [置顶] 将数据导出到excel文件的操作类(用法大全)
- Java POI 导出Oracle数据表写入Excel文件(Excel2007 xlsx)
- 一个导出CListCtrl控件数据到Excel文件的封装函数[r]
- PHP导出MySQL数据到Excel文件
- 在YII中使用PHPExcel扩展导出数据到EXCEL中
- 使用phpexcel导出数据,基本使用方法
- mandys 使用PHPEXcel导出表数据
- 将页面数据导出到excel文件