【问题标题】:Kohana 3 + PhpExcel problem with xls exportKohana 3 + PhpExcel 的 xls 导出问题
【发布时间】:2011-01-10 13:26:59
【问题描述】:

我已将 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...

【问题讨论】:

    标签: kohana kohana-3 phpexcel


    【解决方案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 中发送文件。

    【讨论】:

      【解决方案2】:

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

      我认为您不需要更改请求标头,也许除了字符集编码。

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

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

      【讨论】:

      • 不工作,发送的文件是空的 (0Bytes)。但是感谢有趣的功能,不知道 send_file()...
      【解决方案3】:

      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

      【讨论】:

      • 谢谢,但这些格式没有帮助,输出的文件仍然损坏。现在我以这种方式解决了我的问题:将生成的 xls 保存在服务器上,然后从那里下载超链接...,不理想但现在可以使用...
      【解决方案4】:

      我用这个,它有效。

      $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 我相信。

      【讨论】:

        【解决方案5】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多