2017 © Pedro Peláez
 

symfony-bundle phpspreadsheet-bundle

A Symfony bundle to integrate with PHPOffice's PhpSpreadsheet library

image

yectep/phpspreadsheet-bundle

A Symfony bundle to integrate with PHPOffice's PhpSpreadsheet library

  • Tuesday, July 17, 2018
  • by yectep
  • Repository
  • 3 Watchers
  • 12 Stars
  • 5,526 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 5 Versions
  • 154 % Grown

The README.md

PhpSpreadsheetBundle

This bundle integrates your Symfony 4/5/6/7 app with the PHPOffice PhpSpreadsheet productivity library., (*1)

Requirements

This bundle requires, in addition to prerequisites of each PHPOffice library:, (*2)

* PHP 7.2 or higher
* Symfony 4 or higher

Note: Tags older than v1.0.0 (e.g. v0.2.0) are no longer supported due to deprecated status for both PHP <= 7.1 and Symfony <= 4.4., (*3)

Installation

Use composer to require the latest stable version., (*4)

composer require yectep/phpspreadsheet-bundle

If you're not using Flex, enable the bundle in your AppKernel.php or bundles.php file., (*5)

$bundles = array(
    [...]
    new Yectep\PhpSpreadsheetBundle\PhpSpreadsheetBundle(),
);

Usage

This bundle enables the phpoffice.spreadsheet service., (*6)

See also the official PHPOffice PhpSpreadsheet documentation., (*7)

createSpreadsheet()

Creates an empty \PhpOffice\PhpSpreadsheet\Spreadsheet object, or, if an optional $filename is passed, instantiates the \PhpOffice\PhpSpreadsheet\IOFactory to automatically detect and use the appropriate IWriter class to read the file., (*8)

// In your controller
$newSpreadsheet = $this->get('phpoffice.spreadsheet')->createSpreadsheet();
$existingXlsx   = $this->get('phpoffice.spreadsheet')->createSpreadsheet('/path/to/file.xlsx');

createReader(string $type)

Returns an instance of the \PhpOffice\PhpSpreadsheet\Reader class of the given $type., (*9)

Types are case sensitive. Supported types are:, (*10)

  • Xlsx: Excel 2007
  • Xls: Excel 5/BIFF (95)
  • Xml: Excel 2003 XML
  • Slk: Symbolic Link (SYLK)
  • Ods: Open/Libre Office (ODS)
  • Csv: CSV
  • Html: HTML
$readerXlsx  = $this->get('phpoffice.spreadsheet')->createReader('Xlsx');
$spreadsheet = $readerXlsx->load('/path/to/file.xlsx');

createWriter(Spreadsheet $spreadsheet, string $type)

Given a \PhpOffice\PhpSpreadsheet\Spreadsheet object and a writer $type, returns an instance of a \PhpOffice\PhpSpreadsheet\Writer class for that type., (*11)

In addition the the read types above, these types are additionally supported for writing, if the appropriate PHP libraries are installed., (*12)

  • Tcpdf
  • Mpdf
  • Dompdf
$spreadsheet = $this->get('phpoffice.spreadsheet')->createSpreadsheet();
$spreadsheet->getActiveSheet()->setCellValue('A1', 'Hello world');

$writerXlsx = $this->get('phpoffice.spreadsheet')->createWriter($spreadsheet, 'Xlsx');
$writerXlsx->save('/path/to/destination.xlsx');

Roadmap and Contributions

Contributions are more than welcome. Fork the project, and submit a PR when you're done., (*13)

Remaining todos include:, (*14)

  • Tests and test coverage
  • TravisCI
  • Improved documentation

Symfony serializer

If you are migrating from Symfony Serializer component + CSV encoder - you can use code like, (*15)

$spreadsheet = $this->get('phpoffice.spreadsheet')->createSpreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle($this->filterVars['wareCategory']->getTitle());
$columnsMap = [];
$lineIndex = 2;
foreach ($data as $line) {
   foreach ($line as $columnName=>$columnValue) {
       if (is_int($columnIndex = array_search($columnName, $columnsMap))) {
           $columnIndex++;
       } else {
           $columnsMap[] = $columnName;
           $columnIndex = count($columnsMap);
       }
       $sheet->getCellByColumnAndRow($columnIndex, $lineIndex)->setValue($columnValue);
   }
   $lineIndex++;
}
foreach ($columnsMap as $columnMapId=>$columnTitle) {
   $sheet->getCellByColumnAndRow($columnMapId+1, 1)->setValue($columnTitle);
}
$writer = $this->get('phpoffice.spreadsheet')->createWriter($spreadsheet, 'Xlsx');
ob_start();
$writer->save('php://output');
$excelOutput = ob_get_clean();

return new Response(
   $excelOutput,
   200,
   [
       'content-type'        =>  'text/x-csv; charset=windows-1251',
       'Content-Disposition' => 'attachment; filename="price.xlsx"'
   ]
);

The Versions

17/07 2018

dev-master

9999999-dev

A Symfony bundle to integrate with PHPOffice's PhpSpreadsheet library

  Sources   Download

MIT

The Requires

 

by Chester Li

22/03 2018

v0.0.4

0.0.4.0

A Symfony bundle to integrate with PHPOffice's PhpSpreadsheet library

  Sources   Download

MIT

The Requires

 

by Chester Li

21/03 2018

v0.0.3

0.0.3.0

A Symfony bundle to integrate with PHPOffice's PhpSpreadsheet library

  Sources   Download

MIT

The Requires

 

by Chester Li

15/02 2018

v0.0.2

0.0.2.0

A Symfony bundle to integrate with PHPOffice's PhpSpreadsheet library

  Sources   Download

MIT

The Requires

 

by Chester Li

14/02 2018

v0.0.1

0.0.1.0

A Symfony bundle to integrate with PHPOffice's PhpSpreadsheet library

  Sources   Download

MIT

The Requires

 

by Chester Li