Menu

Tree [15c155] master /
 History

HTTPS access


File Date Author Commit
 Classes 2014-12-30 Krzysztof Ruszczyński Krzysztof Ruszczyński [8a0981] updated version number
 COPYING 2014-12-28 ruszczak ruszczak [7789b0] updatedcontent of lgpl license
 Examples.php 2014-12-30 Krzysztof Ruszczyński Krzysztof Ruszczyński [d42dfb] updated version number
 README.md 2014-12-30 Krzysztof Ruszczyński Krzysztof Ruszczyński [15c155] removed information about errors in demo

Read Me

KrscReports

In order to run, this project needs PHPExcel. Put PHPExcel source in Classes folder. Then load in your browser Examples.php file from the main folder and see the possibilities of this library. Live demo is here: [http://www.ruszczynski.eu/krsc/Examples.php]

The concept of application presented by tutorial

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');


That is all what we need to create a PHPExcel spreadsheet. Look for examples in library code if you want to extend your knowledge. Thanks for reading :)

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.