PHP code example of spiriitlabs / excel-rust

1. Go to this page and download the library: Download spiriitlabs/excel-rust library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spiriitlabs / excel-rust example snippets


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

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');
    }
}

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'
            ),
        );
    }
}

    $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');


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

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