CodeIgniter使用PHPExcel製作Excel

1 下載PHPExcel,將PHPExcel解壓縮到Application的third_party目錄下
2 在libraries目錄下新增 excel.php

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH."/third_party/PHPExcel.php"; 

class Excel extends PHPExcel { 
    public function __construct() { 
        parent::__construct(); 
    } 
}
?>

使用

	function createexcel(){
		// 產生php 範例
		$this->load->library('excel');
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '中文');
        $objPHPExcel->getActiveSheet()->setCellValue('B2', '許');
        $objPHPExcel->getActiveSheet()->setCellValue('C3', 'test3');
        $objPHPExcel->getActiveSheet()->setCellValue('D3', 'test4');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="excel_report_' . date('ymd') . '.xls"');
        // header('Content-Disposition: attachment; filename="file.xls"');
        header('Cache-Control: max-age=0');

        $objWriter->save('php://output');

	}