2. Create reports without using views (old way) or creating own views - krzysztofruszczynski/Krsc-Reports-PHPExcel-Framework GitHub Wiki
Firstly, we have to set instance of PHPExcel class, which would be used inside called classes from KrscReports. We should do that by such command:
KrscReports_Builder_Excel_PHPExcel::setPHPExcelObject( new PHPExcel() );
Next step towards creating a worksheet is to create object, which will store all elements, that we want to display in generated file:
$oElement = new KrscReports_Document_Element();
In that moment, we can add elements to concrete worksheets. For example we want to add a table to sheet named "summary". Firstly, we should create object for that element:
$oElementTable = new KrscReports_Document_Element_Table();
At that moment we decided that we want to display a table, but we haven't decided yet precisiely, what would be the structure of the table (for example if we want a summary under last row for each column or we want first row before column name to use for table description). In order to do that, we have to create and set a builder like that:
$oBuilder = new KrscReports_Builder_Excel_PHPExcel_ExampleTable();
In this moment we have to add properties to builder. Firstly, we have to set an object, which would be responsible for creating cells:
$oCell = new KrscReports_Type_Excel_PHPExcel_Cell();
By calling this object, we can set styles for our builder, but using styles would be described later. Now we have to attach cell object to our builder:
$oBuilder->setCellObject( $oCell );
Next issue would be setting data for builder, for example like this:
$oBuilder->setData( array( array( 'First column' => '1', 'Second column' => '2' ),
array( 'First column' => '3', 'Second column' => '4' ) ) );
For creating a table, data structure should obey certain pattern. It should be an array, which subsequent elements are arrays (without fixed keys). Each subarray keys are names of columns and their values are what would be displayed in worksheet. Subarray keys are used to make header rows for tables.
Having properly configured builder we can attach it now to element:
$oElementTable->setBuilder( $oBuilder );
In that moment we have a proper element ready to be inserted in worksheet. Here we use element created at the beginning and add our table element to it:
$oElement->addElement( $oElementTable, 'first_one' );
Second parameter is the name of sheet, in which element would be displayed. If this parameter is omitted, default name for sheet would be used. It is possible to place many elements in one sheet. It is also possible to create many sheets. If user doesn't change it, the elements would be displayed one after the other.
After that, we have to construct document from our objects. In order to do that, we execute those 3 commands (order is important):
$oElement->beforeConstructDocument();
$oElement->constructDocument();
$oElement->afterConstructDocument();
In that moment we have correctly constructed PHPExcel object and we are ready to create a file from it. To do so, we have to create writer and save it, just like that:
$oWriter = PHPExcel_IOFactory::createWriter( KrscReports_Builder_Excel_PHPExcel::getPHPExcelObject(), 'Excel2007');
$oWriter->save('php://output');