Codeigniter+PHPExcel实现导出数据到Excel文件_PHP教程
codeigniter
phpexcel
PHPExcel是用来操作OfficeExcel文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格。而Codeigniter是一个功能强大的PHP框架。二者结合就能起到非常棒的效果啦!
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)
代码如下:
复制代码 代码如下:
classTable_exportextendsCI_Controller{
function__construct()
{
parent :: __construct();
// Hereyoushouldaddsomesortofuservalidation
// topreventstrangersfrompullingyourtabledata
}
functionindex($table_name)
{
$query = $this -> db -> get($table_name);
if(!$query)
returnfalse;
// StartingthePHPExcellibrary
$this -> load -> library('PHPExcel');
$this -> load -> library('PHPExcel/IOFactory');
$objPHPExcel = newPHPExcel();
$objPHPExcel -> getProperties() -> setTitle("export") -> setDescription("none");
$objPHPExcel -> setActiveSheetIndex(0);
// Fieldnamesinthefirstrow
$fields = $query -> list_fields();
$col = 0;
foreach($fieldsas$field)
{
$objPHPExcel -> getActiveSheet() -> setCellValueByColumnAndRow($col, 1, $field);
$col++;
}
// Fetchingthetabledata
$row = 2;
foreach($query -> result()as$data)
{
$col = 0;
foreach($fieldsas$field)
{
$objPHPExcel -> getActiveSheet() -> setCellValueByColumnAndRow($col, $row, $data -> $field);
$col++;
}
$row++;
}
$objPHPExcel -> setActiveSheetIndex(0);
$objWriter = IOFactory :: createWriter($objPHPExcel, 'Excel5');
// Sendingheaderstoforcetheusertodownloadthefile
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');
}
}
function__construct()
{
parent :: __construct();
// Hereyoushouldaddsomesortofuservalidation
// topreventstrangersfrompullingyourtabledata
}
functionindex($table_name)
{
$query = $this -> db -> get($table_name);
if(!$query)
returnfalse;
// StartingthePHPExcellibrary
$this -> load -> library('PHPExcel');
$this -> load -> library('PHPExcel/IOFactory');
$objPHPExcel = newPHPExcel();
$objPHPExcel -> getProperties() -> setTitle("export") -> setDescription("none");
$objPHPExcel -> setActiveSheetIndex(0);
// Fieldnamesinthefirstrow
$fields = $query -> list_fields();
$col = 0;
foreach($fieldsas$field)
{
$objPHPExcel -> getActiveSheet() -> setCellValueByColumnAndRow($col, 1, $field);
$col++;
}
// Fetchingthetabledata
$row = 2;
foreach($query -> result()as$data)
{
$col = 0;
foreach($fieldsas$field)
{
$objPHPExcel -> getActiveSheet() -> setCellValueByColumnAndRow($col, $row, $data -> $field);
$col++;
}
$row++;
}
$objPHPExcel -> setActiveSheetIndex(0);
$objWriter = IOFactory :: createWriter($objPHPExcel, 'Excel5');
// Sendingheaderstoforcetheusertodownloadthefile
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文件了。
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP開發:使用 CodeIgniter 實作 MVC 模式和 RESTful API

在CodeIgniter框架中使用資料庫查詢建構器(Query Builder)的方法
