elearning-ag/exceldatatables

此包已被废弃,不再维护。作者建议使用 svrnm/exceldatatables 包。

在不改变文件其他属性的情况下,替换 Excel 工作簿 (.xlsx) 中的工作表。

v0.2.2 2017-04-04 19:19 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:38:30 UTC


README

在不改变文件其他属性的情况下,替换 Excel 工作簿 (.xlsx) 中的工作表。

许可协议

本程序是免费软件;有关详细信息,请参阅 LICENSE。

详细信息

此库的主要目的是在不修改其他组件的情况下向现有 Excel 文件添加一个“数据表”。如果您有一个模板文件,例如包含“高级”功能(如图表、数据透视表、宏)的报告,并且您想在 PHP 应用程序中更改基础数据表,这将特别有用。

设置

使用 composer 将此仓库添加到您的依赖项中

{
	"require": {
		"svrnm/exceldatatables": "dev-master"
	}
}

如果您使用 laravel 框架,您还可以将 ServiceProvider 添加到您的 app/config.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