CI CodeIgniter framework inside Phpexcel Use | Export data to an Excel file

Source: Internet
Author: User

CI CodeIgniter framework inside the use of Phpexcel | Export data to Excel file, there is a need for friends to refer to.


CI framework inside Phpexcel use | Export data to Excel file 1. Ready to begin ...


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:
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)
{
$this->load->database ();
$query = $this->db->query ("select * from ' $table _name ' WHERE del= 1");
$query = mb_convert_encoding ("gb2312", "UTF-8", $query);
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)
->setcellvalue (' A1 ', iconv (' GBK ', ' utf-8 ', ' Chinese Hello '))
->setcellvalue (' B2 ', ' world! ')
->setcellvalue (' C1 ', ' Hello ');
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 ');
Send headers force users to download files
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 ');
}
}
?>
Join the database has a table named products, this time you can access the Http://www.yoursite.com/table_export/index/products export Excel file
Source
CodeIgniter, Phpexcel
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.