在所有的php office库(http://www.21doc.net/php/awesomephp#Office),以前用得最多的版本是PHPOffice/PHPExcel(https://github.com/PHPOffice/PHPExcel),不过该库最后更新的版本为2015发布的1.8.1版本,已经终止维护,官方也不建议用户继续使用。
官方不维护PHPExcel后,新起一个项目叫PhpSpreadsheet(https://github.com/PHPOffice/PhpSpreadsheet)。新项目使用了大量的php新特性,比如命名空间,PSR标准,性能也比PHPExcel高了不少。
在使用时,由于PhpSpreadsheet是在原PHPExcel基础上开发的,PhpSpreadsheet接口api操作上也和旧的PHPExcel相似,而且支持更多的格式。
整合到yii2框架
下载phpspreadsheet,当前最新版本1.5.0
方式一,composer下载
composer require phpoffice/phpspreadsheet
方式二,对于部分不能用composer顺利下载的用户,可以手工下载源码
PhpSpreadsheet
下载地址:https://github.com/PHPOffice/PhpSpreadsheet/releases
simple-cache (注意:PhpSpreadsheet的依赖库,必须也下载)
下载地址:https://github.com/php-fig/simple-cache/releases
把下载的文件放置在下边目录,如下图:
phpspreadsheet类库命名方式符合 PSR-4 标准,使用 Yii 官方的自动加载器来自动加载这些类。 你只需给他们的每个根命名空间声明一下根路径别名。
main.php
<?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功能。
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,生成的文件内容如下:
来源:oschina
链接:https://my.oschina.net/u/117179/blog/2253032