Kohana 3 + PhpExcel problem with xls export
I have integrated PHPExcel library with Kohana 3, and have some problem wiht output xls file. When I tried create xls file on server (save on server file system) everything is all right, but when 开发者_运维知识库i tried output it with header(), than file is corrupted and show me in Excel some weird characters.
My code in controller/action_index:
$this->auto_render = FALSE;
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello world!');
$type = 'xls';
$mimes = Kohana::config('mimes');
$mime = $mimes[$type][0];
$this->request->headers['Content-Type'] = "$mime; charset=utf-8;";
$this->request->headers['Content-Disposition'] = 'attachment;filename="01simple.xls"';
$this->request->headers['Cache-Control'] = 'max-age=0';
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save("php://output");`
Thank you for help and sorry for my english.
PS: when i try output pdf same way, everything looks all right, problem is only with xls and xlsx...
I use kohana 3.2, and the following:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'Hello world!');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$this->response->body( $objWriter->save('php://output'));
$this->response->send_file(TRUE, '01simple.xls');
Notice the $this->response->body()
and $this->response->send_file()
.
This makes sense to me, you want to send a file in the RESPONSE, not send a file in the REQUEST.
Kohana has a really nice function to force downloads called "send_file". Try this:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello world!');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$this->request->response = $objWriter->save('php://output');
$this->request->send_file(TRUE, '01simple.xls');
I don't think you need to change the request headers, except for the charset encoding maybe.
Let Kohana do the magic (it checks the MIME type by the filename "01simple.xls" for example) ;). By the way, you don't need to disable auto_render since the send_file() function does it :)
More details: http://kohanaframework.org/guide/api/Request#send_file
See this article.
For Excel5 format you must use:
$this->request->headers['Content-Type'] = 'application/vnd.ms-excel';
For Excel2007 format:
$this->request->headers['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
Or can use ready-made module kohana-phpexcel.
I use this, and it works.
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
// Save to the output buffer. Internally the writer creates a temporary
// file to zip it up, then copies it to the php output buffer/stream.
$writer->save('php://output');
$mime = File::mime_by_ext(strtolower(pathinfo($filename, PATHINFO_EXTENSION)));
$size = ob_get_length();
$this->response->header('content-disposition', 'attachment; filename="'.$filename.'"');
$this->response->header('content-type', $mime );
$this->response->header('content-length', (string) $size);
// Send all headers now
$this->response->send_headers();
I can't see how the other answers would work as ->save('php://output') doesn't return anything, so $this->response->body() gets nothing and simply acts as a getter - effectively a NOP.
If you're calling this from a Controller_Template class, you need to ensure that auto_render is set to false I believe.
It's a far stretch, but I read a problem about this on github. The quick fix proposed is to change the header form something xls-like to text/csv
. So in your code try this:
$this->request->headers['Content-Type'] = "text/csv; charset=utf-8;";
or
$this->request->headers['Content-Type'] = "text/csv;";
精彩评论