Codeigniter利用PHPExcel导出Excel文件
发布:smiling 来源: PHP粉丝网 添加日期:2014-07-26 14:43:14 浏览: 评论:0
1.准备工作
下载PHPExcel:http://phpexcel.codeplex.com
这是个强大的Excel库,这里只演示导出Excel文件的功能,其中的大部分功能可能都用不着.
2. 安装PHPExcel到Codeigniter
1) 解压压缩包里的Classes文件夹中的内容到applicationlibraries目录下,目录结构如下:
-- applicationlibrariesPHPExcel.php
-- applicationlibrariesPHPExcel (文件夹)
2)修改applicationlibrariesPHPExcelIOFactory.php 文件
-- 将其类名从PHPExcel_IOFactory改为IOFactory,遵从CI类命名规则.
-- 将其构造函数改为public
3. 安装完毕,写一个导出excel的控制器(Controller),代码如下:
- 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');
- }
- }
方法二,代码如下:
- Excel Plugin
- The following plugin will generate a tab-delimited file, and feed it to the client as an Excel file.
- $this->load->plugin('to_excel');
- $this->db->use_table('tablename');
- $this->db->select('field1', 'field2');
- // run joins, order by, where, or anything else here
- $query = $this->db->get();
- to_excel($query, ['filename']); // filename is optional, without it, the plugin will default to 'exceloutput'
- So you could run:
- to_excel($query, 'myfile'); // outputs myfile.xls
- to_excel($query); // outputs exceloutput.xls
- // you could also use a model here
- to_excel($this->model_name->functioncall());
- /system/plugins/to_excel_pi.php
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- /*
- * Excel library for Code Igniter applications
- * Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
- */
- function to_excel($query, $filename='exceloutput')
- {
- $headers = ''; // just creating the var for field headers to append to below
- $data = ''; // just creating the var for field data to append to below
- $obj =& get_instance();
- $fields = $query->field_data();
- if ($query->num_rows() == 0) {
- echo '<p>The table appears to have no data.</p>';
- } else {
- foreach ($fields as $field) {
- $headers .= $field->name . "t";
- }
- foreach ($query->result() as $row) {
- $line = '';
- foreach($row as $value) {
- if ((!isset($value)) OR ($value == "")) {
- $value = "t";
- } else {
- $value = str_replace('"', '""', $value);
- $value = '"' . $value . '"' . "t";
- }
- $line .= $value;
- }
- $data .= trim($line)."n";
- }
- $data = str_replace("r","",$data);
- header("Content-type: application/x-msdownload");
- header("Content-Disposition: attachment; filename=$filename.xls");
- echo "$headersn$data";
- }
- }
- ?>
Tags: Codeigniter PHPExcel Excel文件
- 上一篇:PHP导入和导出CSV文件实现程序
- 下一篇:QQ登录集成到自己网站php代码
相关文章
- ·CodeIgniter创建一个简单的注册系统(2014-07-19)
- ·CodeIgniter框架钩子机制实现方法【hooks类】(2018-11-15)
- ·codeigniter使用技巧批量插入数据实例方法分享(2020-08-23)
- ·codeigniter自带数据库类使用方法说明(2020-11-05)
- ·CodeIgniter框架提示Disallowed Key Characters的解决办法(2020-11-19)
- ·Codeigniter中禁止A Database Error Occurred错误提示的方法(2021-02-16)
- ·使用配置类定义Codeigniter全局变量(2021-02-16)
- ·codeigniter数据库操作函数汇总(2021-02-16)
- ·Codeigniter操作数据库表的优化写法总结(2021-02-16)
- ·Codeigniter注册登录代码示例(2021-02-16)
- ·使用CodeIgniter的类库做图片上传(2021-02-16)
- ·codeigniter集成ucenter1.6双向通信的解决办法(2021-02-16)
- ·PHP通过PHPExcel类导入导出excel(2014-06-25)
- ·PHPExcel生成和读取Excel文件实例程序(2014-07-18)
- ·phpExcel数据内存溢出解决办法(2014-07-30)
- ·PHPEXCEL导入excel表格生成数组(2014-08-26)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)