在全部的php office庫(http://www.21doc.net/php/awesomephp#Office),之前用得最多的版本是PHPOffice/PHPExcel(https://github.com/PHPOffice/PHPExcel),不過該庫最後更新的版本爲2015發佈的1.8.1版本,已經終止維護,官方也不建議用戶繼續使用。php
官方不維護PHPExcel後,新起一個項目叫PhpSpreadsheet(https://github.com/PHPOffice/PhpSpreadsheet)。新項目使用了大量的php新特性,好比命名空間,PSR標準,性能也比PHPExcel高了很多。git
在使用時,因爲PhpSpreadsheet是在原PHPExcel基礎上開發的,PhpSpreadsheet接口api操做上也和舊的PHPExcel類似,並且支持更多的格式。github
整合到yii2框架web
下載phpspreadsheet,當前最新版本1.5.0
方式一,composer下載npm
composer require phpoffice/phpspreadsheet
方式二,對於部分不能用composer順利下載的用戶,能夠手工下載源碼
PhpSpreadsheet
下載地址:https://github.com/PHPOffice/PhpSpreadsheet/releasesapi
simple-cache (注意:PhpSpreadsheet的依賴庫,必須也下載)
下載地址:https://github.com/php-fig/simple-cache/releasesbash
把下載的文件放置在下邊目錄,以下圖:yii2
phpspreadsheet類庫命名方式符合 PSR-4 標準,使用 Yii 官方的自動加載器來自動加載這些類。 你只需給他們的每一個根命名空間聲明一下根路徑別名。
main.phpapp
<?php return [ 'aliases' => [ '@bower' => '@vendor/bower-asset', '@npm' => '@vendor/npm-asset', '@Psr/SimpleCache' => '@vendor/psr/simple-cache/src', '@PhpOffice' => '@vendor/phpoffice/phpspreadsheet/src', ], 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', ...... ];
PhpSpreadsheet自帶不少例子代碼,參考phpoffice\phpspreadsheet\samples\Basic\01_Simple_download_xlsx.php實現一個http導出excel功能。composer
yii2中導出excel實現:
ExcelController.php
<?php namespace frontend\controllers; use Yii; use yii\web\Controller; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; class ExcelController extends Controller { public function actionIndex(){ // Create new Spreadsheet object $spreadsheet = new Spreadsheet(); // Set document properties $spreadsheet->getProperties()->setCreator('Maarten Balliauw') ->setLastModifiedBy('Maarten Balliauw') ->setTitle('Office 2007 XLSX Test Document') ->setSubject('Office 2007 XLSX Test Document') ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.') ->setKeywords('office 2007 openxml php') ->setCategory('Test result file'); // Add some data $spreadsheet->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); // Miscellaneous glyphs, UTF-8 $spreadsheet->setActiveSheetIndex(0) ->setCellValue('A4', 'Miscellaneous glyphs') ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç'); // Rename worksheet $spreadsheet->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $spreadsheet->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Xlsx) 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 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); exit; } }
訪問http://localhost/yii2/frontend/web/index.php?r=excel/index,生成的文件內容以下: