Structures_DataGrid_Renderer_XLS: I just get empty files
| From: | Markus Ernst | Date: | Mon, 26 Feb 2007 09:49:35 +0000 |
| Subject: | Structures_DataGrid_Renderer_XLS: I just get empty files | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-26679@lists.php.net to get a copy of this message | ||
Hello!
I use Structures_DataGrid for exporting database records to Excel. Anyway, all I get is an empty file which neither Excel nor OpenOffice can read, while the same works without problems with the CSV renderer. Here is my code with some test data:
<?php
// select output format
$suffix = 'xls';
// Initiate and fill datagrid
require_once 'Structures/DataGrid.php';
$datagrid =& new Structures_DataGrid();
$data = array(
array(2 => 'Spears', 3 => 'Britney'),
array(2 => 'Williams', 3 => 'Robbie'),
array(2 => 'Jagger', 3 => 'Mick')
);
$datagrid->bind($data);
// Set columns
$datagrid->addColumn(new Structures_DataGrid_Column('First Name', 3));
$datagrid->addColumn(new Structures_DataGrid_Column('Last Name', 2));
// Set options
$fileformat = strtoupper($suffix);
$filename = 'userlist_'.date('Y-m-d_H-i');
switch ($suffix) {
case 'xls':
$options = array(
'filename' => $filename.'.xls'
);
$content_type = 'application/vnd.ms-excel';
break;
case 'csv':
$options = array(
'delimiter' => ';'
);
$content_type = 'text/csv';
break;
}
$options['encoding'] = 'utf-8';
// Output
header('Content-type: '.$content_type.'; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'.'.$suffix.'"');
$renderer =& $datagrid->setRenderer(constant('DATAGRID_RENDER_'.$fileformat), $options);
$datagrid->render($renderer, $options);
exit;
?>
If I change $suffix to 'csv' I get a proper CSV file, so the problem must be somewhere in the rendering part. Also, the problem is not in the duplicate headers; the XLS file remains empty also when I remove them.
(Sidenote: The CSV could actually do the job, except for non-ASCII characters. Excel just opens it as Latin-1, so the user would need to open it in OpenOffice, choose the correct delimiter and utf-8 encoding and save it to Excel format, which is too complicated, as the application is intended to be used by non-techies.)
Does anybody see the mistake I made in my code? Or is there some special trick needed to make the XLS renderer work?
Thanks for any hint!
Markus