Wrapper for PHPExcel library. Helps to create simple PHPExcel documents easier.
Pure PHPExcel | With php-excel-builder |
---|---|
// Initializing new sheet
$sheet = new \PHPExcel_Worksheet(null, 'New list');
// Set sheet content
$sheet->fromArray([
['ID', 'Name', 'Link'],
[1, 'First', 'http://domain.com'],
[2, 'Second', 'https://example.com']
]);
// Set width to a column
$sheet->getColumnDimension('C')->setWidth(70);
// Convert columns to hyperlinks
for ($i=2; $i<4; $i++) {
$cell = $sheet
->getCellByColumnAndRow(2, $i);
$cell
->getHyperlink()
->setUrl($cell->getValue());
}
// Initializing excel document
$excel = new \PHPExcel();
$excel->removeSheetByIndex(0);
$excel->addSheet($sheet);
// Saving excel document
$writer = new \PHPExcel_Writer_Excel2007($excel);
$writer->save('document.xlsx'); |
// Initializing new sheet
$sheet = SheetBuilder::create('New list')
// Set sheet content
->setHeader(['ID', 'Name', 'Link'])
->setData([
[1, 'First', 'http://domain.com'],
[2, 'Second', 'https://example.com']
])
// Set width to a column
->setColumnWidth('C', 70)
// Define columns to be converted to hyperlinks
->setUrlColumn(2);
// Initializing excel document
ExcelBuilder::create()
->addSheet($sheet)
// ...and saving it
->save('document.xlsx');
|
Wrapper is about 2 classes,
SheetBuilder - constructs \PHPExcel_Worksheet object
,
and ExcelBuilder - constructs \PHPExcel
object and contain collection of SheetBuilder`s.
Return wrapped \PHPExcel_Worksheet
object.
Define first line of sheet.
Header will not be toched by methods such as setUrlColumns()
and setColumnsTypes()
.
Sheet has no header by default.
Define actual rows and columns of sheet.
Provide indexes of columns to define which columns will be converted as hyperlink. All cells (except header) in provided columns will be converted. Columns indexes begin with zero.
SheetBuilder::create('List')
->setData([
['http://domain.com', 'Content', 'http://example.com']
])
->setUrlColumns([0, 2]);
Same as setUrlColumns()
but for single column.
Provide indexes of columns columns to define which of PHPExcel_Cell_DataType::TYPE_*
data-type will be used by each column.
Columns indexes begin with zero.
SheetBuilder::create('List')
->setData([
['100', 'Numbers will be shown as strings', '350']
])
->setColumnsTypes([0, 2]);
Same as setColumnTypes()
but for single column.
Wrapper for \PHPExcel_Worksheet_ColumnDimension::setWidth()
. Set width of column by index.
Same as setColumnWidth()
but for many columns.
Return wrapped \PHPExcel
object.
Select class of writer will be used to save the document.
Any of \PHPExcel_Writer_Abstract
subclasses can be used.
Default writer is \PHPExcel_Writer_Excel2007
.
// Use CSV writer
ExcelBuilder::create()
->setWriterType(PHPExcel_Writer_CSV::class);
Set collection of SheetBuilder
objects to current builder.
Before builder is saved, it builds all previously added sheets and composes them into document.
Add sheet to collection at provided index. If index is null, just add provided sheet to collection.
Build and save the document as provided filename.
ExcelBuilder::create()
->addSheet($sheet)
->save(__DIR__ . '/../document.xlsx');
Licensed under the MIT license.