1

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

我在控制器/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 ......

4

5 回答 5

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

这对我来说很有意义,您想在响应中发送文件,而不是在请求中发送文件。

于 2012-05-24T08:53:10.530 回答
0

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

于 2011-01-15T03:56:16.433 回答
0

这篇文章

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

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

Excel2007格式:

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

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

于 2011-02-10T07:42:33.677 回答
0

这是一个很长的范围,但我在github上读到了一个关于这个的问题。建议的快速解决方法是将标题形式更改为类似 xls 的内容text/csv。所以在你的代码中试试这个:

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

或者

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

于 2016-07-26T21:41:12.993 回答
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();

我看不到其他答案将如何工作,因为 ->save('php://output') 不返回任何内容,因此 $this->response->body() 什么也得不到,只是充当吸气剂 -实际上是一个 NOP。

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

于 2016-07-18T05:23:18.730 回答