Raw Output

142 views
Skip to first unread message

Aaron Clausen

unread,
Aug 27, 2013, 12:51:04 PM8/27/13
to joomla-de...@googlegroups.com
Is it possible to generate raw output for a view other than view.raw.php?

Specifically I'm trying desparately to get PHPExcel working properly
but overriding headers and writing to php://output is giving me
corrupted files.

--
Aaron Clausen
mightym...@gmail.com

Aaron Clausen

unread,
Aug 27, 2013, 1:05:08 PM8/27/13
to joomla-de...@googlegroups.com
Okay, to make things a bit more specific, this is my code:

class TestComponentViewReport extends JViewLegacy {
public function display($tpl = null) {

jimport('phpexcel.library.PHPExcel');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objPHPExcel = new PHPExcel();

$this->assignRef('PHPExcel', $objPHPExcel);
$tpl = 'excel';
parent::display($tpl);
}
}

I also have a tmpl/default_excel.php that looks something like this:

<?php

$this->PHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');

$this->PHPExcel->getActiveSheet()->setTitle('Simple');
$this->PHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007');
$objWriter->save('php://output');

Aaron Clausen

Matt Thomas

unread,
Aug 27, 2013, 1:12:36 PM8/27/13
to Joomla! General Development
I'm not familiar with PHPExcel, but assuming that $objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007'); creates the Excel file, I think you can do something like:

<?php

$this->PHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');

$this->PHPExcel->getActiveSheet()->setTitle('Simple');
$this->PHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007');
echo $objWriter,
JFactory::getApplication()->close();


Best,

Matt Thomas
Founder betweenbrain
Phone: 203.632.9322
Twitter: @betweenbrain



--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Bakual

unread,
Aug 27, 2013, 1:41:37 PM8/27/13
to joomla-de...@googlegroups.com
I'm using PHPExcel in an extension I write for my company. You can have a look at it here: https://github.com/Bakual/Wissensmatrix, to be more precise this view: https://github.com/Bakual/Wissensmatrix/tree/master/site/views/reportfwigteam.
 
I'm not using the raw format, I use an "xls" format. But it doesn't really make a difference. Fact is you need to use a view.raw.php or in my case a view.xls.php or it will not work. Using the default view.html.php will generate the HTML headers and corrupt the file.

Aaron Clausen

unread,
Aug 27, 2013, 2:20:35 PM8/27/13
to joomla-de...@googlegroups.com
Okay, I think I solved my problem. This appears to be a buffering
problem. It's probably something specific to PHP 5.3.3 on my copy of
Squeeze. I did turn output_buffering=off in the php.ini file, but it
had no effect. In the end I stumbled on this rather simple solution.
Before I invoke $objWriter->save('php://output'), I put in a
ob_end_clean(). Something like:

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('Simple');
ob_end_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-disposition: attachment; filename=test.xlsx");
header("Content-Transfer-Encoding: binary ");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

When I do that, an uncorrupted xls file is downloaded that I can open in Excel.

Thanks everyone for your help! Another mystery solved...

Aaron Clausen

Bakual

unread,
Aug 28, 2013, 7:30:05 AM8/28/13
to joomla-de...@googlegroups.com
Just a thought: There may also be issues with system or content plugins which are not properly coded. If those produce a line break, it may corrupt your file as well. Also sometimes those plugins don't check for the document type and produce HTML and/or Javascript code even if it's not a HTML document. Those will break the file as well.

Maybe you faced this and your ob_end_clean does remove this output.

Aaron Clausen

unread,
Aug 28, 2013, 11:39:12 AM8/28/13
to joomla-de...@googlegroups.com
This is a bare install of 3.1. I haven't put any other plugins in, and
the only extension of any kind is the PHPExcel library itself. Since I
don't see how ob_end_clean() can hurt, I'll happily leave it in there.

Thanks for the tips everyone! You folks are an invaluable resource for
a guy trying to get his head around Joomla 3.

Aaron
Reply all
Reply to author
Forward
0 new messages