Codeigniter+PHPExcel實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)到Excel文件
PHPExcel是用來操作OfficeExcel文檔的一個(gè)PHP類庫,它基于微軟的OpenXML標(biāo)準(zhǔn)和PHP語言??梢允褂盟鼇碜x取、寫入不同格式的電子表格。而Codeigniter是一個(gè)功能強(qiáng)大的PHP框架。二者結(jié)合就能起到非常棒的效果啦!
1.準(zhǔn)備工作
下載PHPExcel:http://phpexcel.codeplex.com
這是個(gè)強(qiáng)大的Excel庫,這里只演示導(dǎo)出Excel文件的功能,其中的大部分功能可能都用不著。
2.安裝PHPExcel到Codeigniter
1)解壓壓縮包里的Classes文件夾中的內(nèi)容到application\libraries\目錄下,目錄結(jié)構(gòu)如下:
--application\libraries\PHPExcel.php
--application\libraries\PHPExcel(文件夾)
2)修改application\libraries\PHPExcel\IOFactory.php文件
--將其類名從PHPExcel_IOFactory改為IOFactory,遵從CI類命名規(guī)則。
--將其構(gòu)造函數(shù)改為public
3.安裝完畢,寫一個(gè)導(dǎo)出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');
}
}
4.測(cè)試
加入數(shù)據(jù)庫有表名為products,此時(shí)可以訪問http://www.yoursite.com/table_export/index/products導(dǎo)出Excel文件了。
- 使用PHPExcel實(shí)現(xiàn)數(shù)據(jù)批量導(dǎo)出為excel表格的方法(必看)
- 利用phpExcel實(shí)現(xiàn)Excel數(shù)據(jù)的導(dǎo)入導(dǎo)出(全步驟詳細(xì)解析)
- ThinkPHP使用PHPExcel實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出完整實(shí)例
- php實(shí)現(xiàn)利用phpexcel導(dǎo)出數(shù)據(jù)
- Yii中使用PHPExcel導(dǎo)出Excel的方法
- PHPExcel導(dǎo)出2003和2007的excel文檔功能示例
- PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法
- thinkphp3.2中實(shí)現(xiàn)phpexcel導(dǎo)出帶生成圖片示例
- 基于PHPExcel的常用方法總結(jié)
- PHPExcel實(shí)現(xiàn)表格導(dǎo)出功能示例【帶有多個(gè)工作sheet】
相關(guān)文章
PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能
這篇文章主要介紹了PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能,通過ajax實(shí)現(xiàn)主界面,php處理上傳文件,具體實(shí)例代碼大家跟隨腳本之家小編一起看看吧2018-06-06phpstorm配置php運(yùn)行環(huán)境的詳細(xì)步驟
這篇文章主要介紹了phpstorm配置php運(yùn)行環(huán)境的詳細(xì)步驟,首先安裝phpstrom,按照提示的步驟一步一步來就行,文中給大家介紹了phpstorm的簡單配置,需要的朋友可以參考下2023-09-09phpstorm動(dòng)態(tài)調(diào)試環(huán)境部署過程
這篇文章主要介紹了php代碼審計(jì)phpstorm動(dòng)態(tài)調(diào)試的過程,xdebug調(diào)試調(diào)試環(huán)境部署的操作過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04thinkPHP框架實(shí)現(xiàn)類似java過濾器的簡單方法示例
這篇文章主要介紹了thinkPHP框架實(shí)現(xiàn)類似java過濾器的簡單方法,結(jié)合實(shí)例形式分析了thinkPHP基于繼承實(shí)現(xiàn)的登錄驗(yàn)證功能相關(guān)操作方法,需要的朋友可以參考下2018-09-09Ubuntu中啟用php的mail()函數(shù)并解決發(fā)送郵件速度慢問題
本文主要給大家介紹的是在Ubuntu下安裝sendmail的方法,以及啟用sendmail之后,php發(fā)送郵件緩慢的原因及解決方法,有需要的小伙伴可以參考下。2015-03-03destoon安全設(shè)置中需要設(shè)置可寫權(quán)限的目錄及文件
這篇文章主要介紹了destoon安全設(shè)置中需要設(shè)置可寫權(quán)限的目錄及文件,對(duì)于安全設(shè)置非常重要!需要的朋友可以參考下2014-06-06