1. Go to this page and download the library: Download oskargunther/excel-writer 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/ */
oskargunther / excel-writer example snippets
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
....
new \ExcelWriterBundle\ExcelWriterBundle(),
];
}
}
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');
$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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.