Phpexcel is a PHP class library for manipulating Officeexcel documents, based on Microsoft's OPENXML Standard and PHP language. You can use it to read and write spreadsheets in different formats. And CodeIgniter is a powerful PHP framework. They combine to make a great effect!
1. Preparatory work
Download phpexcel:http://phpexcel.codeplex.com
This is a powerful Excel library that only demonstrates the ability to export Excel files, most of which may not be possible.
2. Install Phpexcel to CodeIgniter
1) Unzip the contents of the Classes folder in the package into the Application\libraries\ directory, the directory structure is as follows:
--application\libraries\phpexcel.php
--application\libraries\phpexcel (folder)
2) Modify the application\libraries\phpexcel\iofactory.php file
--Change its class name from Phpexcel_iofactory to Iofactory and follow the CI class naming convention.
--change its constructor to public
3. After installation, write a controller that exports Excel
The code is as follows:
Copy CodeThe code is as follows: 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 ();
SetDescription ("None"), GetProperties-Settitle ("Export"), $objPHPExcel,
Setactivesheetindex, $objPHPExcel (0);
Fieldnamesinthefirstrow
$fields = $query-List_fields ();
$col = 0;
foreach ($fieldsas $field)
{
Setcellvaluebycolumnandrow ($col, 1, $field), Getactivesheet, $objPHPExcel
$col + +;
}
Fetchingthetabledata
$row = 2;
foreach ($query, result () as$data)
{
$col = 0;
foreach ($fieldsas $field)
{
Setcellvaluebycolumnandrow ($col, $row, $data-$field), Getactivesheet, $objPHPExcel
$col + +;
}
$row + +;
}
Setactivesheetindex, $objPHPExcel (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. Testing
Join the database with the table named products, you can access the Http://www.yoursite.com/table_export/index/products export Excel file now.
http://www.bkjia.com/PHPjc/788630.html www.bkjia.com true http://www.bkjia.com/PHPjc/788630.html techarticle Phpexcel is a PHP class library for manipulating Officeexcel documents, based on Microsoft's OPENXML Standard and PHP language. You can use it to read and write spreadsheets in different formats. and Codeign ...