定制 emizoripx/office-php74 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

emizoripx/office-php74

Composer 安装命令:

composer require emizoripx/office-php74

包简介

Document generation from existing Excel templates | Export tables to Excel (Grids)

README 文档

README

Clone of https://github.com/AnourValar/office for internal compatibility with PHP7.4

Office: Documents | Reports | Grids

Installation

Minimal

composer require anourvalar/office

To work with default driver - Phpspreadsheet is required:

composer require phpoffice/phpspreadsheet "^1.23"

To save documents as PDF - Mpdf is required:

composer require mpdf/mpdf: "^8.0"

Generate a document from an XLSX (Excel) template

One-dimensional table

template1.xlsx:

Demo

$data = [
    // scalar
    'vat' => 'No',
    'total' => [
        'price' => 2004.14,
        'qty' => 3,
    ],

    // one-dimensional table
    'products' => [
        [
            'name' => 'Product #1',
            'price' => 989,
            'qty' => 1,
            'date' => new \DateTime('2022-03-30'),
        ],
        [
            'name' => 'Product #2',
            'price' => 1015.14,
            'qty' => 2,
            'date' => new \DateTime('2022-03-31'),
        ],
    ],
];

// Save as XLSX (Excel)
(new \EmizorIpx\OfficePhp74\SheetsService())
    ->generate(
        'template1.xlsx', // path to template
        $data // input data
    )
    ->saveAs(
        'generated_document.xlsx', // path to save
        \EmizorIpx\OfficePhp74\Format::Xlsx // save format
    );

// Available formats:
// \EmizorIpx\OfficePhp74\Format::Xlsx
// \EmizorIpx\OfficePhp74\Format::Pdf
// \EmizorIpx\OfficePhp74\Format::Html
// \EmizorIpx\OfficePhp74\Format::Ods

generated_document.xlsx:

Demo

The same template with empty data

Demo

Two-dimensional table

template2.xlsx:

Demo

$data = [
    'best_manager' => 'Sveta',

    // two-dimensional table
    'managers' => [
        'titles' => [[ 'William', 'James', 'Sveta' ]],

        'values' => [
            [ // additional row
                'month' => 'January',
                'amount' => [700, 800, 900], // additional columns
            ],
            [
                'month' => 'February',
                'amount' => [7000, 8000, 9000],
            ],
            [
                'month' => 'March',
                'amount' => [70000, 80000, 90000],
            ],
        ],
    ],
];

// Save as XLSX (Excel)
(new \EmizorIpx\OfficePhp74\SheetsService())
    ->generate('template2.xlsx', $data)
    ->saveAs('generated_document.xlsx'); // second argument (format) is optional

generated_document.xlsx:

Demo

Additional Features

template3.xlsx:

Demo

$data = [
    'foo' => 'Hello',

    'bar' => function (\EmizorIpx\OfficePhp74\Drivers\SheetsInterface $driver, $cell) {
        $driver->insertImage('logo.png', $cell, ['width' => 100, 'offset_y' => -45]);
        return 'Logo!'; // replace marker "[bar]" with return value
    }
];

(new \EmizorIpx\OfficePhp74\SheetsService())
    ->hookValue(function (SheetsInterface $driver, $cell, $value, int $sheetIndex) {
        // Hook will be called for every cell which is changing

        $value .= ' world';
        return $value;
    })
    ->generate(
        'template3.ods', // ods template
        $data,
        true // cells auto format instead of template setup
    )
    ->saveAs('generated_document.xlsx');

// Available hooks:
// hookLoad: Closure(SheetsInterface $driver, string $templateFile, Format $templateFormat)
// hookBefore: Closure(SheetsInterface $driver, array &$data)
// hookValue: Closure(SheetsInterface $driver, string $cell, mixed $value, int $sheetIndex)
// hookAfter: Closure(SheetsInterface $driver)

generated_document.xlsx:

Demo

Dynamic templates

$data = [
    'group1' => [
        'name' => 'Group 1',
        'products' => [
            ['name' => 'Product 1', 'stock' => 101],
            ['name' => 'Product 2', 'stock' => 102],
        ],
    ],
    'group2' => [
        'name' => 'Group 2',
        'products' => [
            ['name' => 'Product 3', 'stock' => 103],
            ['name' => 'Product 4', 'stock' => 104],
        ],
    ],
];

(new \EmizorIpx\OfficePhp74\SheetsService())
    ->hookLoad(function ($driver, string $templateFile, $templateFormat)
    {
        // create empty document instead of using existing
        return $driver->create();
    })
    ->hookBefore(function ($driver, array &$data)
    {
        // place markers on-fly
        $row = 1;
        foreach (array_keys($data) as $group) {
            // group's title
            $driver
                ->setValue("A$row", "[{$group}.name]")
                ->mergeCells("A$row:B$row")
                ->setStyle("A$row", ['align' => 'center', 'bold' => true]);
            $row++;

            // group's products
            $driver
                ->setValue("A$row", "[$group.products.name]")
                ->setValue("B$row", "[$group.products.stock]");
            $row++;
        }
    })
    ->generate('', $data)
    ->saveAs('generated_document.xlsx');

Dynamic template overview

Demo

generated_document.xlsx:

Demo

Merge (union) few documents to a single file

$dataA = ['foo' => 'hello'];
$dataB = ['foo' => 'world'];

$documentA = (new \EmizorIpx\OfficePhp74\SheetsService())->generate('template.xlsx', $dataA);
$documentB = (new \EmizorIpx\OfficePhp74\SheetsService())->generate('template.xlsx', $dataB);

$mixer = new \EmizorIpx\OfficePhp74\Mixer();
$mixer($documentA, $documentB)->saveAs('generated_document.xlsx');

Export table (Grid)

Simple usage

$data = [
    ['William', 3000],
    ['James', 4000],
    ['Sveta', 5000],
];

// Save as XLSX (Excel)
(new \EmizorIpx\OfficePhp74\GridService())
    ->generate(
        ['Name', 'Sales'], // headers
        $data // data
    )
    ->saveAs('generated_grid.xlsx');

generated_grid.xlsx:

Demo

Advanced usage

$headers = [
    ['title' => 'Name', 'width' => 30],
    ['title' => 'Sales'],
];

$data = function () {
    yield ['name' => 'William', 'sales' => 3000];
    yield ['name' => 'James', 'sales' => 4000];
    yield ['name' => 'Sveta', 'sales' => 5000];
};

// Save as XLSX (Excel)
(new \EmizorIpx\OfficePhp74\GridService())
    ->hookHeader(function (GridInterface $driver, mixed $header, $key, $column)
    {
        if (isset($header['width'])) {
            $driver->setWidth($column, $header['width']); // column with fixed width
        } else {
            $driver->autoWidth($column); // column with auto width
        }

        return $header['title'];
    })
    ->hookRow(function (GridInterface $driver, mixed $row, $key)
    {
        return [
            $row['name'],
            $row['sales'],
        ];
    })
    ->hookAfter(function (
        GridInterface $driver,
        string $headersRange,
        string $dataRange,
        string $totalRange,
        array $columns
    ) {
        $driver->setSheetTitle('Foo');

        $driver->setStyle(
            $headersRange, // A1:B1
            ['bold' => true, 'background_color' => 'EEEEEE']
        );

        $driver->setStyle(
            $totalRange, // A1:B4
            ['borders' => true, 'align' => 'left']
        );
    })
    ->generate($headers, $data)
    ->saveAs('generated_grid.xlsx');

generated_grid.xlsx:

Demo

emizoripx/office-php74 适用场景与选型建议

emizoripx/office-php74 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 86 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「excel」 「xls」 「xlsx」 「template」 「pdf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 emizoripx/office-php74 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 emizoripx/office-php74 我们能提供哪些服务?
定制开发 / 二次开发

基于 emizoripx/office-php74 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 86
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-21