使用xls导出的Kohana 3 + PhpExcel问题

时间:2011-01-10 13:26:59

标签: kohana kohana-3 phpexcel

我已经将PHPExcel库与Kohana 3集成在一起,并且在输出xls文件时遇到了一些问题。当我尝试在服务器上创建xls文件(保存在服务器文件系统上)时,一切都很好,但是当我尝试使用header()输出时,文件已损坏,并在Excel中显示一些奇怪的字符。

我在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");`

感谢您的帮助,对不起我的英语。

PS:当我尝试以同样的方式输出pdf时,一切看起来都没问题,问题只出在xls和xlsx ......

5 个答案:

答案 0 :(得分:1)

我使用kohana 3.2,以及以下内容:

$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');

请注意$this->response->body()$this->response->send_file()

这对我来说很有意义,你想在RESPONSE中发送一个文件,而不是在REQUEST中发送一个文件。

答案 1 :(得分:0)

Kohana有一个非常好的功能来强制下载名为“send_file”。试试这个:

$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');

我认为您不需要更改请求标头,除了charset编码可能。

让Kohana做魔术(例如,它通过文件名“01simple.xls”检查MIME类型);)。顺便说一句,你不需要禁用auto_render,因为send_file()函数会这样做:)

更多详情:http://kohanaframework.org/guide/api/Request#send_file

答案 2 :(得分:0)

请参阅this article

对于Excel5格式,您必须使用:

$this->request->headers['Content-Type'] = 'application/vnd.ms-excel';

对于Excel2007格式:

$this->request->headers['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

或者可以使用现成的module kohana-phpexcel

答案 3 :(得分:0)

我用它,它有效。

$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();

我无法看到其他答案如何起作用 - >保存(' php://输出')并不会返回任何内容,因此$ this->响应 - > body()得不到任何东西,只是充当一个吸气剂 - 实际上是一个NOP。

如果你从Controller_Template类调用它,你需要确保auto_render设置为false我相信。

答案 4 :(得分:0)

这是一个很大的延伸,但我在github上看到了这个问题。建议的快速修复是将标题形式从xls-like更改为text/csv。所以在你的代码中试试这个:

$this->request->headers['Content-Type'] = "text/csv; charset=utf-8;";

$this->request->headers['Content-Type'] = "text/csv;";