以前の記事のCodeIgniter3.0版です。

PHPExcelのバージョンは、「1.8.1」

CodeIgniter3.0は、Composerを使用して、インストールしている状態です。
これは、下記の記事を参考にし、ファイルの配置も同じくしています。
http://rdlabo.jp/codeigniter-302.php

GitHubにkenjisさんが作ったパッケージがあるのですが、
何も変更せずに使ってみると、「vfsStream」も入ってきます。
私には必要ないので自前の、composer.json を使っています。

PHPExcelをComposerに追加

PHPExcelもpackagestにあるのでこれを使います。
https://packagist.org/packages/phpoffice/phpexcel

composer.json に phpexcel を追加します。

{
    "require": {
            "codeigniter/framework": "3.0.*",
            "phpoffice/phpexcel": "1.8.*"
    }
}

これで、composer update を実行すると、vendor以下に「phpoffice」が追加され、その中に「phpexcel」が入っています。

Controllerを作成

以前のものがほぼそのまま使えます。composerでPHPExcelをインストールしているのでautoloadされている前提です。

ベースとしているソースは、PHPExcelのサンプルソースです。

class Sample01 extends CI_Controller{

    public function index()
    {
        $this->download_excel();
    }
 
    private function download_excel()
    {
 
        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();
        // Set properties
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                    ->setLastModifiedBy("Maarten Balliauw")
                    ->setTitle("Office 2013 XLSX Test Document")
                    ->setSubject("Office 2013 XLSX Test Document")
                    ->setDescription("Test document for Office 2013 XLSX, generated using PHP classes.")
                    ->setKeywords("office 2013 openxml php")
                    ->setCategory("Test result file");
 
        // Add some data
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A1', '日本語')
                    ->setCellValue('B2', 'テストです。')
                    ->setCellValue('C3', 'CodeIgniter3.0で作成しています。')
                    ->setCellValue('D4', 'いかがですか');
 
        // Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle('Simple');

        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client’s web browser (Excel2007)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="01simple.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        exit;
    }
}

これで、sample01/indexにアクセスするとExcelファイルのダウンロードができます。
そのまんまなんでCodeIgniter的な使い方が他にあるだろうか??