Codeigniter+PHPExcel Implement Exporting Data to Excel File

  • 2021-06-29 10:27:15
  • OfStack

PHPExcel is an 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.Codeigniter is a powerful PHP framework.A combination of the two will do a great job!

1. Preparations

Download PHPExcel:http://phpexcel.codeplex.com
This is a powerful Excel library, which only demonstrates the ability to export Excel files, most of which may not be needed.

2. Install PHPExcel to Codeigniter

1) Unzip the contents of the Classes folder in the package into the applicationlibrariesdirectory with the following directory structure:
--application\libraries\PHPExcel.php
--applicationlibrariesPHPExcel (folder)
2) Modify the applicationlibrariesPHPExcelIOFactory.php file
--From PHPExcel_IOFactory was changed to IOFactory, following CI class naming rules.
--Change its constructor to public

3. After installation, write a controller that exports excel (Controller)

The code is as follows:

<?php
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. Testing

Join the database with the table name products, where you can access http://www.yoursite.com/table_export/index/products exported the Excel file.


Related articles: