svrnm / exceldatatables
在不更改文件其他属性的情况下,替换 Excel 工作簿 (.xlsx) 中的工作表
v0.3.0
2024-07-10 14:07 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- mikey179/vfsstream: 1.3.*@dev
- phpspec/phpspec: ~2.0
This package is auto-updated.
Last update: 2024-09-16 15:11:02 UTC
README
在不更改文件其他属性的情况下,替换 Excel 工作簿 (.xlsx
) 中的工作表。
许可证
本程序是自由软件;有关更多详细信息,请参阅LICENSE。
详情
此库的主要目的是在不修改任何其他组件的情况下,向现有 Excel 文件添加“数据表”。如果您有一个模板文件,例如用于包含“高级”功能(如图表、数据透视表、宏)的报表,并且希望在 PHP 应用程序中更改基础数据表,这将特别有用。
设置
使用 composer 将此存储库添加到您的依赖项
{ "require": { "svrnm/exceldatatables": "dev-master" } }
如果您使用 Laravel 框架,您还可以将 ServiceProvider
添加到您的 config/app.php
... 'providers' => array( ... 'Svrnm\ExcelDataTables\ExcelDataTablesServiceProvider' ) ...
示例
以下示例演示了如何使用 ExcelDataTables。以下内容也包含在 examples/
文件夹中的 example.php
<?php require_once('../vendor/autoload.php'); // Create a new instance $dataTable = new Svrnm\ExcelDataTables\ExcelDataTable(); // Specify the source file $in = 'spec.xlsx'; // Specify the output file $out = 'test.xlsx'; // Specify the data for the worksheet. $data = array( array("Date" => new \DateTime('2014-01-01 13:00:00'), "Value 1" => 0, "Value 2" => 1), array("Date" => new \DateTime('2014-01-02 14:00:00'), "Value 1" => 1, "Value 2" => 0), array("Date" => new \DateTime('2014-01-03 15:00:00'), "Value 1" => 2, "Value 2" => -1), array("Date" => new \DateTime('2014-01-04 16:00:00'), "Value 1" => 3, "Value 2" => -2), array("Date" => new \DateTime('2014-01-05 17:00:00'), "Value 1" => 4, "Value 2" => -3), ); // Attach the data table and copy the new xlsx file to the output file. $dataTable->showHeaders()->addRows($data)->attachToFile($in, $out); ?>
在此示例中,方法 attachToFile
创建了一个新的 Excel 文件。如果您在 Web 应用程序中使用此库,您可能更喜欢 fillXLSX()
函数,该函数返回 Excel 文档的字符串表示形式。以下示例在 Laravel 应用程序中演示了这种情况
<?php class ReportController extends Controller { protected $dataTable; public function __construct(\Svrnm\ExcelDataTables\ExcelDataTable $dataTable) { $this->dataTable = $dataTable; } public function show($month) { $data = DB::select('select date,value1,value2 from reporting where MONTH(date) = ?', array($month)); $path = storage_path() . '/reports/example.xlsx'; $xlsx = $this->dataTable->showHeaders()->addRows($data)->fillXLSX($path); return Response::make($xlsx, 200, array( 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Content-Disposition' => 'attachment; filename="report.xlsx"', 'Content-Length' => strlen($xlsx) )); // ... } } ?>
联系
如有任何问题,您可以联系 Severin Neumann severin.neumann@altmuehlnet.de。