Home >Backend Development >PHP Tutorial >Use of PHPExcel in CI Codeigniter framework | Export data to Excel file

Use of PHPExcel in CI Codeigniter framework | Export data to Excel file

WBOY
WBOYOriginal
2016-07-25 08:46:251192browse

Usage of PHPExcel in CI Codeigniter framework|Export data to Excel file, friends in need can refer to it.


Use of PHPExcel in CI framework | Export data to Excel file 1. Ready to start...


Download PHPExcel: http://phpexcel.codeplex.com
This is a powerful Excel library. Here we only demonstrate the function of exporting Excel files. Most of the functions may not be needed.
2. Install PHPExcel to Codeigniter
1) Unzip the contents of the Classes folder in the compressed package to the applicationlibraries directory. The directory structure is as follows:
-- applicationlibrariesPHPExcel.php
-- applicationlibrariesPHPExcel (folder)
2) Modify the applicationlibrariesPHPExcelIOFactory.php file
-- Change its class name from PHPExcel_IOFactory to IOFactory to follow the CI class naming rules.
-- Change its constructor to public
3. After the installation is complete, write a controller (Controller) for exporting 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 a header to force the user to download the file
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');
}
}
?>
Add a table named products to the database. At this time, you can visit http://www.yoursite.com/table_export/index/products to export the Excel file.
Source
Codeigniter, PHPExcel


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn