queencitycodefactory / cakespreadsheet
CakePHP 3 PHP Spreadsheet
3.9.1
2022-10-28 14:07 UTC
Requires
- php: >=7.2.0
- cakephp/cakephp: ^3.5.10
- phpoffice/phpspreadsheet: ^1.6.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^3.0
- phpunit/phpunit: ^5.7 | ^6.0
This package is auto-updated.
Last update: 2024-09-19 15:27:56 UTC
README
CakeSpreadsheet 是一个 CakePHP 3.x 插件,使用 PHPOffice/PhpSpreadsheet 生成 Excel 文件(xlsx 格式)。
需求
- CakePHP 3.x
- PHP 7.2
安装
[使用 Composer]
composer require queencitycodefactory/cakespreadsheet
启用插件
在您的应用程序的 config/bootstrap.php
文件中加载插件
Plugin::load('CakeSpreadsheet', ['bootstrap' => true, 'routes' => true]);
使用方法
首先,您需要为 xlsx
扩展设置扩展解析。为此,您需要在您的 config/routes.php
文件中添加以下内容
// Set this before you specify any routes Router::extensions('xlsx');
接下来,我们需要在您的 Controller 中添加一个 viewClassMap 条目。您可以将以下内容放在您的 AppController 中
public $components = [ 'RequestHandler' => [ 'viewClassMap' => [ 'xlsx' => 'CakeSpreadsheet.Spreadsheet', ], ] ];
每个应用程序 必须 有一个 xlsx 布局。以下是一个简单的布局,可以放置在 src/Template/Layout/xlsx/default.ctp
<?= $this->fetch('content') ?>
最后,您可以通过 .xlsx 扩展链接到当前页面。这假设您已经在特定控制器模板目录中创建了一个 xlsx/index.ctp
文件
$this->Html->link('Excel file', ['_ext' => 'xlsx']);
在您的视图文件中,您可以通过使用 $spreadsheet = $this->getSpreadsheet()
来访问 PhpOffice\PhpSpreadsheet\Spreadsheet
库。请参阅 PhpOffice\PhpSpreadsheet 文档以了解如何使用 PhpOffice\PhpSpreadsheet
。