Making it work in CakePHP

Jan 18, 2009 at 8:37 PM
I'm trying to get PHPExcel to work in CakePHP.

I've declared the class (and the files) as a 3rd party class (using vendor classes).

Then I used the standard example code just to see if I could get something as output.

I've declared an controller action called "export" which takes care of the creation of the file.

The browser indeeds sends me a file, but it'sz completely blank.

here the code I use in my controller action:

	function __doExport($exportType) {
$findParams = array('conditions' => $this->Session->read('conditions'),
'order' => 'Entity.name');
$Entities = $this->Entity->find('all', $findParams);

// create Excel export object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("CratosManager");
$objPHPExcel->getProperties()->setLastModifiedBy("CratosManager");
// set values
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->setCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->setCellValue('D2', 'world!');
// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="export.xls"');
header('Cache-Control: max-age=0');
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

$objWriter->save('php://output');
}
I must be doing something wrong I guess, but I don't see the issue. Can someone tell me what I'm doing wrong here?

thank
Developer
Jan 18, 2009 at 11:23 PM
If you are sending xls file to the browser, the appropriate Content-Type is application/vnd.ms-excel. But this is probably not the error. I tried you script and it is working for me!

Can you try to save the file normally to disk and get that working first?




Jan 19, 2009 at 6:48 PM
Thanks Koyama for the tip, but as you thought already it didn't solve the issue.

I have changed the header & Writer lines to:

   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

Within my CakePHP controller I've added the line just before the ones above (for debugging):

debug($objPHPExcel);

this is what I get (which seems ok):

app/controllers/companies_controller.php (line 136)

PHPExcel Object
(
[_properties:private] => PHPExcel_DocumentProperties Object
(
[_creator:private] => CratosManager
[_lastModifiedBy:private] => CratosManager
[_created:private] => 1232394188
[_modified:private] => 1232394188
[_title:private] => Untitled Spreadsheet
[_description:private] =>
[_subject:private] =>
[_keywords:private] =>
[_category:private] =>
)

[_security:private] => PHPExcel_DocumentSecurity Object
(
[_lockRevision:private] =>
[_lockStructure:private] =>
[_lockWindows:private] =>
[_revisionsPassword:private] =>
[_workbookPassword:private] =>
)

[_workSheetCollection:private] => Array
(
[0] => PHPExcel_Worksheet Object
(
[_parent:private] => PHPExcel Object
*RECURSION*
[_cellCollection:private] => Array
(
[A1] => PHPExcel_Cell Object
(
[_column:private] => A
[_row:private] => 1
[_value:private] => Hello
[_dataType:private] => s
[_dataValidation:private] =>
[_hyperlink:private] =>
[_parent:private] => PHPExcel_Worksheet Object
*RECURSION*
)

[B2] => PHPExcel_Cell Object
(
[_column:private] => B
[_row:private] => 2
[_value:private] => world!
[_dataType:private] => s
[_dataValidation:private] =>
[_hyperlink:private] =>
[_parent:private] => PHPExcel_Worksheet Object
*RECURSION*
)

[C1] => PHPExcel_Cell Object
(
[_column:private] => C
[_row:private] => 1
[_value:private] => Hello
[_dataType:private] => s
[_dataValidation:private] =>
[_hyperlink:private] =>
[_parent:private] => PHPExcel_Worksheet Object
*RECURSION*
)

[D2] => PHPExcel_Cell Object
(
[_column:private] => D
[_row:private] => 2
[_value:private] => world!
[_dataType:private] => s
[_dataValidation:private] =>
[_hyperlink:private] =>
[_parent:private] => PHPExcel_Worksheet Object
*RECURSION*
)

)

[_rowDimensions:private] => Array
(
)

[_defaultRowDimension:private] => PHPExcel_Worksheet_RowDimension Object
(
[_rowIndex:private] =>
[_rowHeight:private] => -1
[_visible:private] => 1
[_outlineLevel:private] => 0
[_collapsed:private] =>
)

[_columnDimensions:private] => Array
(
[A] => PHPExcel_Worksheet_ColumnDimension Object
(
[_columnIndex:private] => A
[_width:private] => -1
[_autoSize:private] => 1
[_visible:private] => 1
[_outlineLevel:private] => 0
[_collapsed:private] =>
)

[B] => PHPExcel_Worksheet_ColumnDimension Object
(
[_columnIndex:private] => B
[_width:private] => -1
[_autoSize:private] => 1
[_visible:private] => 1
[_outlineLevel:private] => 0
[_collapsed:private] =>
)

[C] => PHPExcel_Worksheet_ColumnDimension Object
(
[_columnIndex:private] => C
[_width:private] => -1
[_autoSize:private] => 1
[_visible:private] => 1
[_outlineLevel:private] => 0
[_collapsed:private] =>
)

[D] => PHPExcel_Worksheet_ColumnDimension Object
(
[_columnIndex:private] => D
[_width:private] => -1
[_autoSize:private] => 1
[_visible:private] => 1
[_outlineLevel:private] => 0
[_collapsed:private] =>
)

)

[_defaultColumnDimension:private] => PHPExcel_Worksheet_ColumnDimension Object
(
[_columnIndex:private] =>
[_width:private] => -1
[_autoSize:private] =>
[_visible:private] => 1
[_outlineLevel:private] => 0
[_collapsed:private] =>
)

[_drawingCollection:private] => ArrayObject Object
(
)

[_title:private] => Worksheet
[_pageSetup:private] => PHPExcel_Worksheet_PageSetup Object
(
[_paperSize:private] => 1
[_orientation:private] => default
[_scale:private] =>
[_fitToHeight:private] =>
[_fitToWidth:private] =>
[_columnsToRepeatAtLeft:private] => Array
(
[0] =>
[1] =>
)

[_rowsToRepeatAtTop:private] => Array
(
[0] => 0
[1] => 0
)

[_horizontalCentered:private] =>
[_verticalCentered:private] =>
[_printArea:private] =>
)

[_pageMargins:private] => PHPExcel_Worksheet_PageMargins Object
(
[_left:private] => 0.7
[_right:private] => 0.7
[_top:private] => 0.75
[_bottom:private] => 0.75
[_header:private] => 0.3
[_footer:private] => 0.3
)

[_headerFooter:private] => PHPExcel_Worksheet_HeaderFooter Object
(
[_oddHeader:private] =>
[_oddFooter:private] =>
[_evenHeader:private] =>
[_evenFooter:private] =>
[_firstHeader:private] =>
[_firstFooter:private] =>
[_differentOddEven:private] =>
[_differentFirst:private] =>
[_scaleWithDocument:private] => 1
[_alignWithMargins:private] => 1
[_headerFooterImages:private] => Array
(
)

)

[_sheetView:private] => PHPExcel_Worksheet_SheetView Object
(
[_zoomScale:private] => 100
[_zoomScaleNormal:private] => 100
)

[_protection:private] => PHPExcel_Worksheet_Protection Object
(
[_sheet:private] =>
[_objects:private] =>
[_scenarios:private] =>
[_formatCells:private] =>
[_formatColumns:private] =>
[_formatRows:private] =>
[_insertColumns:private] =>
[_insertRows:private] =>
[_insertHyperlinks:private] =>
[_deleteColumns:private] =>
[_deleteRows:private] =>
[_selectLockedCells:private] =>
[_sort:private] =>
[_autoFilter:private] =>
[_pivotTables:private] =>
[_selectUnlockedCells:private] =>
[_password:private] =>
)

[_styles:private] => Array
(
[default] => PHPExcel_Style Object
(
[_font:private] =>
[_fill:private] =>
[_borders:private] =>
[_alignment:private] =>
[_numberFormat:private] =>
[_conditionalStyles:private] => Array
(
)

[_protection:private] =>
[_hashIndex:private] =>
)

[gray125] => PHPExcel_Style Object
(
[_font:private] =>
[_fill:private] => PHPExcel_Style_Fill Object
(
[_fillType:private] => gray125
[_rotation:private] => 0
[_startColor:private] => PHPExcel_Style_Color Object
(
[_argb:private] => FFFFFFFF
[_hashIndex:private] =>
)

[_endColor:private] => PHPExcel_Style_Color Object
(
[_argb:private] => FF000000
[_hashIndex:private] =>
)

[_parent:private] =>
[_parentPropertyName:private] => _fill
[_hashIndex:private] =>
)

[_borders:private] =>
[_alignment:private] =>
[_numberFormat:private] =>
[_conditionalStyles:private] => Array
(
)

[_protection:private] =>
[_hashIndex:private] =>
)

)

[_cellCollectionIsSorted:private] =>
[_breaks:private] => Array
(
)

[_mergeCells:private] => Array
(
)

[_protectedCells:private] => Array
(
)

[_autoFilter:private] =>
[_freezePane:private] =>
[_showGridlines:private] => 1
[_printGridlines:private] =>
[_showSummaryBelow:private] => 1
[_showSummaryRight:private] => 1
[_comments:private] => Array
(
)

[_selectedCell:private] => A1
[_hashIndex:private] =>
)

)

[_activeSheetIndex:private] => 0
[_namedRanges:private] => Array
(
)

[_log] =>
)
But still no luck. I'm now trying to get some debug output from the save function in the Excel2007 Writer, but without any luck so far.

Jan 19, 2009 at 6:57 PM
By the way:

I've tried with the Excel5 writer as well: same thing = nothing.
No entries in any log or error message.

I've checked if my PHP supported XML & ZIP: it does.

I'm still trying to get some debug output from within the class function: no luck so far.
Jan 19, 2009 at 7:25 PM
Update: I've tried to run the code outside an CakePHP controlled path, so it is plain PHP code.

Guess what: it doesn't work.

Now I'm thinking: would it have to do anything with the webserver used? Lighttpd...

Any idea?
Jan 19, 2009 at 7:53 PM
I've tried with an exact copy of the archive with some changes:

copied the original classes/PHPExcel to /data/www/test/dev/
copied the original classes/PHPExcel.php to /data/www/test/dev/

created an test.php in /data/www/test/dev. the content of test.php is an exact copy of tests/01simple.php example file

When running the test.php browser stays blank and no file created.

This confirms to me that this is a bug because of the lighttpd webserver.

I have opened Issue 8965 for this in the Issue list.

Hopefully a quick solution can be found