Académique Documents
Professionnel Documents
Culture Documents
ExportDatabaseDatatoExcelwithCodeigniterandPHPExcelAnomalousAnomalous
Home
The Team
Services
Our Work
InHouse
Blog
Contact
Posted: June 24, 2014 By: admin Categories: Blog, codeigniter, How To, technical
Comments: 0
1/8
21/12/2014
ExportDatabaseDatatoExcelwithCodeigniterandPHPExcelAnomalousAnomalous
Wehadaneedtoexportdatabasedatafromawebapplicationtoexcelweweredeveloping
usingthecodeignitorframework.Theworstthingthatfrustratesmewithtutorialsonlinethereis
alwaysanassumptionoftheprojectssettingsandtheseareomittedIwilltryandexplain
everythingIhaveinstalled.
ProjectAssumptions(Thesearepartofthecoreandusingintheproject).
1. CodeigniterBaseModelMyCodeIgniterBaseModelisanextendedCI_Modelclassto
useinyourCodeIgniterapplications.ItprovidesafullCRUDbasetomakedeveloping
databaseinteractionseasierandquicker,aswellasaneventbasedobserversystem,in
modeldatavalidation,intelligenttablenameguessingandsoftdelete.
2. IalsoextendedtheCI_ControllerandCalleditFront_controller()
3. CodeigniterVersion2.1.4
4. DownloadacopyofPHPExcelfromGithub
FileandfolderInstructions
1. Createanewfolderexportinyourpublic_htmlfolderorthewwwfolderonyourserver.
2. ExtractthecontentsofthePHPExcelfileyoudownloadedorclonedandcopythecontents
oftheclassesdirectoryintoapplication/third_Partyfolder,thethird_partyfoldershouldhave
afoldercalledPHPExcelandafilecalledPHPExcel.php.
3. Createanewfileinapplication/librariescalledexcel.php
4. Createanewfileinapplication/controllercalledexport.php(oranynamethatyoulike)
Codeforthefileexcel.phpinapplication/libraries
http://www.anomalous.co.za/exportdatabasedataexcelcodeigniterandphpexcel/
2/8
21/12/2014
ExportDatabaseDatatoExcelwithCodeigniterandPHPExcelAnomalousAnomalous
1 <?php
2
3 if (!defined('BASEPATH'))
4
exit('No direct script access allowed');
5
6 class Excel {
7
8
private $excel;
9
10
public function __construct() {
11
require_once APPPATH . 'third_party/PHPExcel.php';
12
$this->excel = new PHPExcel();
13
}
14
15
public function load($path) {
16
$objReader = PHPExcel_IOFactory::createReader('Excel5');
17
$this->excel = $objReader->load($path);
18
}
19
20
public function save($path) {
21
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
22
$objWriter->save($path);
23
}
24
25
public function stream($filename, $data = null) {
26
if ($data != null) {
27
$col = 'A';
28
foreach ($data[0] as $key => $val) {
29
$objRichText = new PHPExcel_RichText();
30
$objPayable = $objRichText->createTextRun(str_replace("_", " ", $key
31
$this->excel->getActiveSheet()->getCell($col . '1')->setValue($objRich
32
$col++;
33
}
34
$rowNumber = 2;
Codeforthefileexport.phpinapplication/controller
35
foreach ($data as $row) {
36
$col = 'A';
37
foreach ($row as $cell) {
38
$this->excel->getActiveSheet()->setCellValue($col . $rowNumber
39
$col++;
40
}
41
$rowNumber++;
42
}
43
}
44
header('Content-type: application/ms-excel');
45
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
46
header("Cache-control: private");
47
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
48
$objWriter->save("export/$filename");
49
header("location: " . base_url() . "export/$filename");
50
unlink(base_url() . "export/$filename");
51
}
http://www.anomalous.co.za/exportdatabasedataexcelcodeigniterandphpexcel/
3/8
21/12/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ExportDatabaseDatatoExcelwithCodeigniterandPHPExcelAnomalousAnomalous
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Export extends Front_Controller {
public function __construct() {
parent::__construct();
// Load the Library
$this->load->library("excel");
// Load the Model
$this->load->model("Your_model_name");
}
public function index() {
$this->excel->setActiveSheetIndex(0);
// Gets all the data using MY_Model.php
$data = $this->Your_model_name->get_all();
$this->excel->stream('name_of_file.xls', $data);
}
}
IhopethishelpssomeonewhentheywanttoexportdatatoExcelwithcodeigniterand
PHPExcel.
Share this:
Facebook
Twitter
Google
LinkedIn
Email
More
http://www.anomalous.co.za/exportdatabasedataexcelcodeigniterandphpexcel/
4/8
21/12/2014
ExportDatabaseDatatoExcelwithCodeigniterandPHPExcelAnomalousAnomalous
Related
Codeigniterhasanewhome
October20,2014
In"PHP"
Toolsweusetomanagethe
office.
July23,2013
In"Blog"
Thestateofthenation.
September19,2014
In"Blog"
Tags: codeigniter,MY_Model,PHPExcel
SHARE THIS :
Like
Tweet
NO COMMENTS SO FAR!
LEAVE A COMMENT
Name(required)
Email(required)
http://www.anomalous.co.za/exportdatabasedataexcelcodeigniterandphpexcel/
5/8
21/12/2014
ExportDatabaseDatatoExcelwithCodeigniterandPHPExcelAnomalousAnomalous
Message
SENDMESSAGE
AreyouHuman
+1=nine
Notifymeofnewpostsbyemail.
http://www.anomalous.co.za/exportdatabasedataexcelcodeigniterandphpexcel/
6/8
21/12/2014
ExportDatabaseDatatoExcelwithCodeigniterandPHPExcelAnomalousAnomalous
http://www.anomalous.co.za/exportdatabasedataexcelcodeigniterandphpexcel/
7/8
21/12/2014
ExportDatabaseDatatoExcelwithCodeigniterandPHPExcelAnomalousAnomalous
HOME
THE TEAM
SERVICES
OUR WORK
INHOUSE
BLOG
CONTACT
http://www.anomalous.co.za/exportdatabasedataexcelcodeigniterandphpexcel/
8/8