secit-pl/simple-excel-export

简单的 Excel 导出器

2.0.0 2024-08-29 19:41 UTC

This package is not auto-updated.

Last update: 2024-09-12 19:46:18 UTC


README

一个简单的库,允许快速创建 MS Excel 导出。

主要功能

  • 简单
  • 多工作表支持
  • 可以通过 PropertyAccess 组件PHP 回调 访问值。
  • 支持 XLS、XLSX 和 CSV 格式(在创建新的 \SecIT\SimpleExcelExport\Excel 对象时通过传递第二个参数)
  • Symfony 4+ 兼容(getResponse() 返回有效的 Symfony 响应

安全警告

注意

此包使用的 PHPOffice/PhpSpreadsheet 库在版本 <2.2.1 中存在严重的安全漏洞。请尽快将此包更新到 2.0.0 或更高版本,该版本允许仅安装安全的 PHPOffice/PhpSpreadsheet 版本。更多详细信息请见此处 https://github.com/PHPOffice/PhpSpreadsheet/security/advisories/GHSA-ghg6-32f9-2jp7

安装

从命令行运行

$ composer require secit-pl/simple-excel-export

使用方法

基本示例

将 Excel 文件作为响应发送给用户

<?php

use SecIT\SimpleExcelExport\Excel;

// example data
$data = [
    'Simple array example' => [
        ['col1' => 123, 'col2' => 321],
        ['col1' => 234, 'col2' => 345],
    ],
];

$excel = new Excel('test', Excel::OUTPUT_XLSX);
$excel->setColumnsAutoSizingEnabled(true);

$excel->addSheet('Simple array example')
    ->setColumn('Column 1', '[col1]') // use Symfony property access component notation or callback
    ->setColumn('Column 2', '[col2]');

// get response (Symfony compatible) 
$response = $excel->getResponse($data)

// and sent it to the browser
$response->send();

创建 Excel 文件

<?php

use SecIT\SimpleExcelExport\Excel;

// example data
$data = [
    'Simple array example' => [
        ['col1' => 123, 'col2' => 321],
        ['col1' => 234, 'col2' => 345],
    ],
];

$excel = new Excel('test', Excel::OUTPUT_XLSX);
$excel->setColumnsAutoSizingEnabled(true);

$excel->addSheet('Simple array example')
    ->setColumn('Column 1', '[col1]') // use Symfony property access component notation or callback
    ->setColumn('Column 2', '[col2]');

// get file 
$splFileObject = $excel->getFile('/path/to/the/file.xlsx', $data);

高级示例

<?php

use SecIT\SimpleExcelExport\Excel;

// Excel data
// data class used in this example
class ExampleUser {
    public $name;
    public $surname;
    public $parent;

    public function __construct($name, $surname, ExampleUser $parent = null) {
        $this->name = $name;
        $this->surname = $surname;
        $this->parent = $parent;
    }
}

// the data
$data = [
    'Simple array example' => [
        ['col1' => 123, 'col2' => 321],
        ['col1' => 234, 'col2' => 345],
    ],
    'Filters example' => [
        ['col3' => 'So sad', 'col4' => new \DateTime()],
        ['col3' => 'So happy', 'col4' => new \DateTime('1234-12-11 11:11:22')],
    ],
    'Objects example' => [
        new ExampleUser('John', 'Blue', new ExampleUser('Jan', 'Blue')),
        new ExampleUser('Jack', 'Red', new ExampleUser('Tom', 'Red')),
    ],
    'Callback example' => [
        ['col1' => 1, 'col2' => 2, 'col3' => null],
        ['col1' => 3, 'col2' => 4, 'col3' => null],
    ],
];

// Create the new Excel object
$excel = new Excel('test', Excel::OUTPUT_XLSX);
$excel->setColumnsAutoSizingEnabled(true);

// Simple array example
$excel->addSheet('Simple array example')
    ->setColumn('Column 1', '[col1]')
    ->setColumn('Column 2', '[col2]');

// Filters example
$excel->addSheet('Filters example')
    ->setColumn('Column 3', '[col3]', [
        new Excel\Filter\PregReplaceFilter('/sad/', 'happy'),
    ])
    ->setColumn('Column 4', '[col4]', [
        new Excel\Filter\DateTimeFilter('d.m.Y'),
    ]);

// Objects example
$excel->addSheet('Objects example')
    ->setColumn('Name', 'name')
    ->setColumn('Surname', 'surname')
    ->setColumn('Parent name', 'parent.name')
;

// Callback example
$excel->addSheet('Callback example')
    ->setColumn('Column 1', '[col1]')
    ->setColumn('Column 2', '[col2]')
    ->setColumn('Column 1 + Column 2', static function ($row) {
        return $row['col1'] + $row['col2'];
    });

// Get response and sent it to the browser
$excel->getResponse($data)
    ->send();

想要支持此包吗?

请考虑在我们的 随机代码生成器 服务 codito.io 上进行操作。

使用 codito.io,您可以免费生成多达 250,000 个您选择的格式代码。您可以将生成的代码用于促销代码(例如,可以打印在包装内部)、序列号、一次性或多次使用的密码、彩票券、折扣代码、优惠券、随机字符串等 - 更多用例请参阅我们的 示例。如果您需要 250,000 个代码还不够,您可以使用我们的 商业代码生成服务

Random Code Generator