rap2hpoutre / fast-excel

🦉 Fast Excel import/export for Laravel
MIT License
2.1k stars 246 forks source link

Allow to add ``OpenSpout\Common\Entity\Cell`` instances as data collection values #306

Open kusab85 opened 1 year ago

kusab85 commented 1 year ago

Add ability to feed FastExcel exporter (directly or via callback) with data consisting of instances of OpenSpout\Common\Entity\Cell, which gives possibility to style every column (or even every cell) independently.

For example code like this:

$money_style = (new Style())->setCellAlignment(CellAlignment::RIGHT)->setFormat('$ # ##0.00');
$data        = [
    [
        'Employee' => 'William Smith',
        'Salary'   => Cell::fromValue(3400.00, $money_style),
        'Tax'      => Cell::fromValue(420.40, $money_style),
    ],
    [
        'Employee' => 'Dany Carey',
        'Salary'   => Cell::fromValue(4300.00, $money_style),
        'Tax'      => Cell::fromValue(220.40, $money_style),
    ],
    [
        'Employee' => 'Matt Cameron',
        'Salary'   => Cell::fromValue(3300.00, $money_style),
        'Tax'      => Cell::fromValue(222.43, $money_style),
    ],
];

(new FastExcel($data))
    ->headerStyle((new Style())->setCellAlignment(CellAlignment::CENTER)->setFontBold())
    ->configureWriterUsing(function ($writer) {
        $writer->getOptions()->setColumnWidth(20, 1);
        $writer->getOptions()->setColumnWidth(10, 2, 3);
    })
    ->export($file);

gives:

obraz

Or code like this:

$data = [
    [
        'col1' => Cell::fromValue('row1 col1', (new Style())->setFontBold()->setCellAlignment(CellAlignment::RIGHT)),
        'col2' => 'row1 col2'
    ],
    [
        'col1' => Cell::fromValue('row2 col1', (new Style())->setFontItalic()->setCellAlignment(CellAlignment::RIGHT)),
        'col2' => Cell::fromValue('row2 col2', (new Style())->setFontSize(20)->setBackgroundColor(Color::LIGHT_GREEN)),
    ],
    [
        'col1' => 'row3 col1',
        'col2' => Cell::fromValue('row3 col2', (new Style())->setBackgroundColor(Color::YELLOW)),
    ],
];

(new FastExcel($data))
    ->headerStyle((new Style())->setCellAlignment(CellAlignment::CENTER)->setFontBold())
    ->rowsStyle((new Style())->setBackgroundColor(Color::LIGHT_BLUE))
    ->configureWriterUsing( function ($writer ) {
        $writer->getOptions()->setColumnWidth(20, 1, 2);
    })
    ->export($file);

gives:

obraz

duypdx commented 6 days ago

@rap2hpoutre Oh I found this very helpful, could you please take a look at it for me?