soluti/excel-generator

Provides a way to generate Excel files.

dev-master 2018-10-04 13:22 UTC

This package is auto-updated.

Last update: 2021-10-04 19:41:27 UTC


README

A library that provides an easy way to export data into XLSX format, including formating, formulas, relations.

Installation

To install SolutiExcelGenerator run the following command

$ php composer.phar require soluti/excel-generator

Basic Usage

  1. Create a Worksheet object, it needs to implement `Soluti\ExcelGenerator\WorksheetInterface, for convenience you can extend the baseSoluti\ExcelGenerator\Worksheet` class. It provides a good starting point. More details on creating a worksheet in the next section.

  2. Create a workbook object and add one or many worksheets

     $workbook = new Workbook();
     $workbook->addWorksheet(
      new CustomWorksheet(
          ...
      )
     );
    
  3. Call the generate method passing the filePath where the xls should be saved (ex:`$workbook->generate(DIR . '/test.xlsx')` )

Creating your Worksheet

In order to create a new Worksheet you have 2 options:

  1. Implement `Soluti\ExcelGenerator\WorksheetInterface`, in this case the only thing you need to do is return a Generator that will output the raw XML for the Worksheet. The `getName()` method returns the name of the Worksheet.

  2. Extend `Soluti\ExcelGenerator\Worksheetthen implementgetName()andgetSheetData()` that returns a Generator for each row of the Sheet body. You can also overwrite the Header or footer sections.

The body of the Worksheet is composed of rows, inside `getSheetData()it is preferable that youyield` each row, to build a row you can use the row helper functions:

  • 
    
  • 
    
  • 
    
  • 
    
  • 
    

After starting a row you need to add cells to it. The following helper static methods exist:

  • 
    
  • 
    
  • 
    
  • 
    
  • 
    

For all methods the `$column` should be the alphabetic column name from Excel - you can use the


### Example of custom worksheet:

```

<?php

namespace App\Excel;

use Soluti\ExcelGenerator\Worksheet\Helper\CellWriter;
use Soluti\ExcelGenerator\Worksheet\Worksheet as BaseWorksheet;

class Worksheet extends BaseWorksheet
{
    const START_COLUMN = 1;

    const FORMAT_NORMAL = 1;
    const FORMAT_HEADER = 2;
    const FORMAT_DATE = 3;

    /** @var string */
    private $name = 'Sheet';

    /** @var $data */
    private $data;

    /**
     * @inheritdoc
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @return \Generator
     * @throws \Soluti\ExcelGenerator\Exception\ExcelWriterException
     */
    public function getSheetData()
    {
        yield '<sheetData>';
        foreach ($this->data as $row) {
            yield from $this->getDataRow($row);
        }
        yield '</sheetData>';
    }

    /**
     * @return mixed
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param mixed $data
     */
    public function setData($data): void
    {
        $this->data = $data;
    }

    /**
     * @param array $row
     *
     * @return \Generator
     * @throws \Soluti\ExcelGenerator\Exception\ExcelWriterException
     */
    private function getDataRow(array $row)
    {
        $this->rowStart();
        $columnIndex = self::START_COLUMN;
        foreach ($row as $key => $value) {
            $this->rowAddCell(
                $this->getCell(
                    CellWriter::getColumnIndex($columnIndex),
                    $this->rowIndex(),
                    $value
                )
            );
            $columnIndex++;
        }

        yield $this->rowEnd();
    }

    /**
     * @param string $column
     * @param int $row
     * @param $value
     * @return string
     */
    protected function getCell(string $column, int $row, $value)
    {
        if (is_numeric($value)) {
            return CellWriter::getNumericCell($column, $row, $value, self::FORMAT_NORMAL);
        }

        if ($value instanceof \DateTime) {
            return CellWriter::getDateCell($column, $row, $value, self::FORMAT_DATE);
        }

        if (is_string($value)) {
            return CellWriter::getInlineCell($column, $row, $value, self::FORMAT_NORMAL);
        }

        throw new \LogicException('Unknown type passed to proxy method');
    }
}


```

## To Do:

- Create a style manager + integrate with Worksheet
- Create a shared string manager + helper methods