Codeigniter+phpexcel implementing export Data to an Excel file _php tutorial

Source: Internet
Author: User
Tags php language naming convention php framework codeigniter
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 ...

  • 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.