You are on page 1of 8

Greyhead.

net

How-to docs and tutorials


ChronoForms How-to doc
Excel export extras
ChronoForms allows you to export records to Excel and CSV formats. This
document has some help tips and suggestions and a recipe for handling large
exports.
Advanced : requires some PHP and MySQL knowledge.
This How-to document links to the recipe Exporting your results to Excel or a
CSV file in Chapter 4 of the ChronoForms 1.3 for Joomla! sites cookbook.

CHRONO
Forms

Greyhead.net

Excel export extras


Diagnosing export problems
The Excel download can have problems with tables that have many
columns or many rows. Sometimes the problems show up as damaged
Excel Files, sometimes the download will time out and may show a
blank page or a PHP Error message.
If you see a blank white page in Joomla! this is usually an indication of
a serious PHP Error that has stopped the Joomla! process from running.
The display of PHP Error messages can usually be set in the Site Global
Configuration panel under Server | Server Settings | Error Reporting.
Usually System Default is a good practical level for a production site;
Maximum may be more useful while you are developing. If this setting
doesnt have the result you want check with your hosting company to
see how they configure PHP Error Reporting.

Hand-coding special exports


You can add PHP code to the OnSubmit boxes to create custom exports.
Use this if you want to pre-process or filter the data in some way.
The ChronoForms code is a good starting point for your hand code. You
will find the Excel and CSV backup code in the administrator/com_
chronocontact/admin.chronocontact.php file. Look for the BackupCSV() and BackupExcel() functions.

Excel export extras

Greyhead.net
ChronoForms makes hand coding fairly simple and straightforward. But
it does require a fair knowledge of PHP and of the Joomla! code base. It
really isnt suitable for beginners to PHP.

Connecting to another MySQL database


If the purpose of the export is to put the data into another database
there can be some problems keeping the two synchronised.
You can save directly from Joomla! to another MySQL database by
creating a second database connection. We usually access the Joomla!
database schema with the code $db =& JFactory::getDBO(); but this
command will also take parameters to make a custom database connection:
<?php
$options = array(
'driver' => 'mysql',
'host' => 'xxx',
'user' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
'prefix' => 'jos_',
);
$db2 =& JDatabase::getInstance($options);
?>

where each of the 'xxx' values needs to be replaced with the appropriate value for the second database.

Excel export extras

Greyhead.net
Connecting to a Microsoft Access database
It is possible to use an OBDC connection to link MySQL and Microsoft
Access databases. See, for example, the article from University College
London at http://www.ucl.ac.uk/is/mysql/access/

Exporting large files


The default ChronoForms export reads all of the database table into
memory and also creates the export file in memory. This means that
large files can quickly blow through the PHP memory limit.
There are a couple of techniques that you can use to help hand-code
more effective exporters.
Write the export file to disk after each record.
Break the table read into chunks instead of reading the whole table at
once. Try chunks of a thousand or so records at a time.
Here is a long code example originally developed to to export records
from a Community Builder file in chunks of 1000.
<?php
// the next row needs to include an array of column names
$table_fields = array( 'column)name_a', 'column_name_b', . . .);
//Prepare the Excel exporter
include_once (JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_chronocontact'.
DS.'excelwriter'.DS.'Writer.php');

Excel export extras

Greyhead.net
$path = JPATH_BASE.DS.'components'.DS.'com_chronocontact'.DS.'uploads'.DS.'temp'.DS;
$file = "Report.xls";
$file_url = JURI::base().'components/com_chronocontact/uploads/temp/'.$file;
$xls = & new Spreadsheet_Excel_Writer($path.$file);
$xls->setVersion(8);
$format = & $xls->addFormat();
$format->setBold();
$format->setColor("blue");
$sheet = & $xls->addWorksheet('Report at '.date("m-d-Y"));
// write the column headers
$titcol = 0;
foreach ( $table_fields as $table_field ) {
$sheet->writeString(0, $titcol, $table_field, $format);
$titcol ++;
}
$db =& JFactory::getDBO();
$datacol = 0;
$rowcount = 1;
// get the number of records to be processed
$query = "
SELECT COUNT(*)
FROM `#__table_name`
";
$db->setQuery($query);
$count = $db->loadResult();
// the value of $count tells us the total number of records

Excel export extras

Greyhead.net
$ceil = ceil($count/1000) - 1;
// create a loop to work in chunks of 1000 records
for ( $k = 0; $k <= $ceil; $k++ ) {
$limitstart = $k*1000;
// get a block of 1000 records
$sql = "
SELECT *
FROM `#__table_name`
LIMIT $limitstart, 1000 ;
";
$db->setQuery($sql);
$datarows = $db->loadObjectList();
$result_array = array();
$i = $k*1000;
foreach ( $datarows as $row ) {
// do any pre-processing of the data here
// putting the required results into $result_array
// write the column entries for this record
foreach ( $table_fields as $table_field ) {
$sheet->writeString($rowcount, $datacol,
$result_array[$table_field], 0);
$datacol ++;
}
$i++;
$datacol = 0;
$rowcount ++;

Excel export extras

Greyhead.net
} // loop to the next record
} // loop to the next block of 1000 records
// close the Excel file
$xls->close();
// show the user a link to download the file
echo "<a href='".$file_url."' >
Your file is ready to download</a>";
?>

Specify a character set for an Excel Backup


If you have problems with special characters then it can help to add this
line to specify the character set to use (utf-8 will often be correct)
$sheet->setInputEncoding('utf-8');

Bob Janes
24 August 2010

Excel export extras

Greyhead.net
Bob Janes is the author of the ChronoForms 1.3 for Joomla!! site Cookbook
published by Packt Publishing. He provides support on the ChronoEngine
forums and works as a developer of custom applications (often using ChronoForms and ChronoConnectivity) for Joomla!! and WordPress.
Bob can be contacted at info@greyhead.net
ChronoConnectivity is a Joomla!! Component that works with ChronoForms
to allow the easy display of lists of records from a database table. ChronoConnectivity and ChronoForms are published by ChronoEngine.com.
This help-sheet was written for ChronoConnectivity 2.0 RC3
ChronoEngine Tutorials by Bob Janes are licensed under a
Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
Disclaimer
To the best of our knowledge and belief the information in this tutorial was
substantially correct at the time of writing. Neither the author, nor ChronoEngine.com shall have neither liability nor responsibility in respect of any loss or
damage caused, or alleged to have been caused, directly or indirectly, by the
information contained in this document.

Excel export extras

You might also like