Add following lines to composer.json
{
"type": "git",
"url": "[email protected]:Tactics/ExcelBundle"
}
"tactics/excel-bundle": "1.0.*"
Getting the excelbuilder service is as easy as:
$builder = $this->get('tactics.excel_writer');
Write an array of values to an excel file
//Make array with some kind of values
$collection = array("First value to write", "Second value to write", ...);
//Offers the excel with values as a file download with filename = awesomeness.xlsx
$builder->getDownloadFromCollection($collection, array(
'filename' => 'awesomeness'
));
Write a collection of objects to an excel file
//get a collection of objects
$collection = $this->getDoctrine()->getManager()->getRepository('SomeBundle:SomeEntity')->findAll();
//Offers the excel with values as a file download with filename = awesomeness.xlsx
$builder->getDownloadFromCollection($collection, array(
'filename' => 'awesomeness',
));
In stead of writing to an empty excel file you can create a template excel file and write to this file.
//$builder = valid ReportExcelWriter.
$excel = $builder->getExcelFromFile('my_templates_file_name', kernel_service);
$builder->writeToSheet($excel->getActiveSheet();
You can extend the default ReportBuilder to add/overwrite (extra) functionality.
class myFunkyBuilderClass extends Tactics\Bundle\ExcelBundle\Writer\ReportExcelWriter { ...
Existing functionality
- get/setActiveCell() (Active cell is coordinate of the cell where content will be written if write method is called)
- get/setFirstCell() (First cell is coordinate, pointer will be set to column value when nextRow method is called)
- nextCell($amount = 1) (sets active cell to current active cell + $amount * cells to the right)
- nextRow($amount = 1)
- write(\PHPExcel_Worksheet $sheet, $value) (writes value to a ExcelSheet)
- setStyleFromCell(\PHPExcel_Worksheet $sheet, $styledCell, $numberOfColumns = 1, $numberOfRows = 1, $merge = false) (copies style from a cell to a new range)
- offerAsXlsxDownload(Response $response, PHPExcel $excel, $filename)
- getExcelFromFileName($fileName, $kernel)
- nextSheet($sheetTitle, $startcell = 'A1')
- getDownloadFromCollection($collection, array $options = array('filename' => 'defaultFileName'))
- writeCollection(\PHPExcel_Worksheet $sheet, $collection, $options = array())
p.e. class myWriteHelper extends Tactics\Bundle\ExcelBundle\Helpers\WriteHelper {
/**
* {@inheritdoc}
*/
public function write(\PHPExcel_Worksheet $sheet, $value) {
...
}
}
//getting the reportwriter with myHelperWriter see todo why this isn't documented yet
AvailableHelpers
- CollectionHelper
- DownloadHelper
- FileReaderHelper
- NavigatorHelper
- ObjectTransFormerHelper
- StylingHelper
- WriteHelper
- define helper classes in configuration so we don't have to invoke the Reportbuilder constructor when overwriting a helper class.
- Bundle should be able to export queries (top priority)
- export to simple csv's
- Maybe be able to read excels/csv's?