spiriitlabs/excel-rust 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

spiriitlabs/excel-rust

最新稳定版本:v0.1.0-beta

Composer 安装命令:

composer require spiriitlabs/excel-rust

包简介

README 文档

README

This PHP library leverages the power of Rust to create Excel files quickly and efficiently.

PHP Version Latest Stable Version CI Tests

Why choose Rust?

Rust is renowned for its execution speed and memory safety, making it an excellent choice for intensive tasks like generating Excel files. By combining PHP for data management with Rust for heavy processing, you get optimal performance without sacrificing the simplicity of PHP code.

How it works

Thanks to a Rust binary, whose source code is available on the GitHub repository, it is possible to generate Excel rows and formats directly from PHP. The data and configuration are transmitted to the binary using the AVRO format (learn more about AVRO).

Once the data is received, the Rust binary uses the excelrust library to create the Excel file.

READY for PROD ?

Yes! We already this in production on large files.

Install

composer require spiriitlabs/excel-rust

Download the last release of the rust binary

Go at https://github.com/SpiriitLabs/excel_gen

Save the binary somewhere

Symfony bundle

Just active the bundle in your bundles.php

Spiriit\Rustsheet\Symfony\Bundle\ExcelRustBundle::class => ['all' => true],

Create a file in the folder packages

config/packages/spiriit_excel_rust.yaml

excel_rust:
  rust_binary: '%rust_binary%' # path to rust binary
  avro_codec: 'null' # default to null, because we don't need to compress the file

You need to use the AsExcelRust attribute

use Spiriit\Rustsheet\Attributes\AsExcelRust;

#[AsExcelRust(name: 'my_super_excel')] // override name is optionnal
class MyExcel implements ExcelInterface 
{
...
}
class MyController
{
    public function __invoke(ExcelRust $excelRust)
    {
        $output = $excelRust->generateExcelFromAvro('MyExcel', __DIR__.'/myexcel_avro.xlsx');
    }
}

Use the library in standalone

Use a builder

First create a class implements ExcelInterface and add attribute 'AsExcelRust'

class MyExcel implements ExcelInterface
{
    public function buildSheet(WorkbookBuilder $builder): void
    {
        $builder = $builder
            ->setDefaultStyleHeader(
                Format::new()
                    ->fontName('Arial')
                    ->fontSize(20)
            );

        $worksheet = Worksheet::new()
            ->setName('Worksheet1');

        $worksheet->writeCell(new Cell(columnIndex: 0, rowIndex: 0, format: null, value: 'Item'));
        $worksheet->writeCell(new Cell(columnIndex: 1, rowIndex: 0, format: null, value: 'Cost'));
        $worksheet->writeCell(new Cell(columnIndex: 2, rowIndex: 0, format: Format::new()->bold(), value: 'Date'));
        $worksheet->writeCell(new Cell(columnIndex: 3, rowIndex: 0, format: Format::new()->bold(), value: 'Autre'));

        $worksheet->writeCell(
            new Cell(
                columnIndex: 0,
                rowIndex: 1,
                format: null,
                value: 'test'
            ),
        );
    }
}

Then create a new instance of ExcelRust and prepare the WorkbookFactory and the excel_rust binary:

    $factory = new \Spiriit\Rustsheet\WorkbookFactory();

    $exportAvro = new ExportAvro(
        schema: file_get_contents(__DIR__.'/../schema.json')
    );
    
    $excelRust = new ExcelRust(
        workbookFactory: $factory,
        rustGenLocation: __DIR__ . '/path/to/excel_gen',
        exportAvro: $exportAvro,
    );
    
    $output = $excelRust->generateExcelFromAvro(new MyExcel(), __DIR__.'/myexcel_avro.xlsx');

From HTML file

    $factory = new \Spiriit\Rustsheet\WorkbookFactory();

    $excelRust = new ExcelRust(
        workbookFactory: $factory,
        rustGenLocation: __DIR__ . '/path/to/excel_gen',
    );
    
    $htmlFile = 'test.html';
    
    $excelRust->generateExcelFromHtml($htmlFile, $output);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2025-02-14