oskargunther/excel-writer

OOP lightweight Excel Writer

0.5.4 2019-12-02 16:31 UTC

This package is auto-updated.

Last update: 2024-03-29 04:18:25 UTC


README

This library uses https://github.com/mk-j/PHP_XLSXWriter as engine
It's only OOP wrapper

As the engine, this library don't waste memory and is written for fast generation of huge sheets

Installation

composer require oskargunther/excel-writer

Symfony > 2.8

    class AppKernel extends Kernel
    {
        public function registerBundles()
        {
            $bundles = [
                ....
                new \ExcelWriterBundle\ExcelWriterBundle(),
            ];
    
        }
    }

Example of usage

include_once 'vendor/autoload.php';

use ExcelWriter\Writer;
use ExcelWriter\Model\HeaderCell;
use ExcelWriter\Model\CellStyle;
use ExcelWriter\Model\Cell;

$writer = Writer::createWriter();


// Writing headers
$headerCellStyle = new CellStyle();
$headerCellStyle->setColor('#FFF');
$headerCellStyle->setFill('#000');

$sheet = 'example';
$header = new Header();
$header
    ->setAutoFilter(true)
    ->setFreezeColumns(0)
    ->setFreezeRows(1)
    ->createCell('Column1', HeaderCell::TYPE_STRING, $headerCellStyle)
    ->addCell((new HeaderCell('Column2', HeaderCell::TYPE_DATETIME, $headerCellStyle))->setColumnWidth(20));

Writer::writeHeaderRow($writer, $sheet, $header);



// Writing data
$data = [
    ['Value1', date('Y-m-d H:i:s')],
    ['Value2', date('Y-m-d H:i:s')]
];
$column2Style = new CellStyle();
$column2Style->setFontStyle(CellStyle::FONT_STYLE_BOLD, CellStyle::FONT_STYLE_ITALIC);
$column2Style->setBorderStyle(CellStyle::BORDER_STYLE_DOTTED);
$column2Style->setBorder(CellStyle::BORDER_BOTTOM, CellStyle::BORDER_LEFT);

foreach ($data as $dataRow) {
    $row = new Row();

    $row
        ->setHeight(30)
        ->createCell($dataRow[0])
        ->addCell((new Cell($dataRow[1], $column2Style)));

    Writer::writeRow($writer, $sheet, $row);
}




// Saving file
Writer::writeToFile($writer, 'example.xlsx');

Example .xlsx File

Using service

$writer = new \ExcelWriterBundle\Service\ExcelWriterService();
// or
$writer = $this->container->get('excel_writer.service');
// Symfony service is not shared so every instance have it's own writer

$writer->writeHeaderRow(...);
$writer->writeRow(...);
$writer->save($filename);