在php中咱們若是要導入excel文件咱們會直接使用php輸出csv文件或者使用phpexcel直接生成excel文件,下面我再來介紹Codeigniter利用PHPExcel導出Excel文件具體實現方法,有須要學習的朋友可參考參考。 php
1. 準備工做
下載PHPExcel:http://phpexcel.codeplex.com/ app
這是個強大的Excel庫,這裏只演示導出Excel文件的功能,其中的大部分功能可能都用不着。 函數
2. 安裝PHPExcel到Codeigniter
1) 解壓壓縮包裏的Classes文件夾中的內容到applicationlibraries目錄下,目錄結構以下: 學習
-- applicationlibrariesPHPExcel.php this
-- applicationlibrariesPHPExcel (文件夾) spa
2)修改applicationlibrariesPHPExcelIOFactory.php 文件 excel
-- 將其類名從PHPExcel_IOFactory改成IOFactory,聽從CI類命名規則。 code
-- 將其構造函數改成public ip
3. 安裝完畢,寫一個導出excel的控制器(Controller) get
<?php 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) { $query = $this -> db -> get($table_name); 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); // 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'); // Sending headers 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'); } }方法二
Excel Plugin
The following plugin will generate a tab-delimited file, and feed it to the client as an Excel file.
$this->load->plugin('to_excel'); $this->db->use_table('tablename'); $this->db->select('field1', 'field2'); // run joins, order by, where, or anything else here $query = $this->db->get(); to_excel($query, ['filename']); // filename is optional, without it, the plugin will default to 'exceloutput'
So you could run:
to_excel($query, 'myfile'); // outputs myfile.xls to_excel($query); // outputs exceloutput.xls // you could also use a model here to_excel($this->model_name->functioncall());/system/plugins/to_excel_pi.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * Excel library for Code Igniter applications * Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006 */ function to_excel($query, $filename = 'exceloutput') { $headers = ''; // just creating the var for field headers to append to below $data = ''; // just creating the var for field data to append to below $obj = &get_instance(); $fields = $query -> field_data(); if ($query -> num_rows() == 0) { echo '<p>The table appears to have no data.</p>'; } else { foreach ($fields as $field) { $headers .= $field -> name . "t"; } foreach ($query -> result() as $row) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "t"; } $line .= $value; } $data .= trim($line) . "n"; } $data = str_replace("r", "", $data); header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=$filename.xls"); echo "$headersn$data"; } } ?>