【问题标题】:Download Excel file using PHPExcel使用 PHPExcel 下载 Excel 文件
【发布时间】:2014-11-04 15:32:57
【问题描述】:

我正在尝试使用 PHPExcel 下载 excel 文件,就像它很好地下载了 excel 文件,但 excel 文件中的数据都是废话.. 这不是我所期望的。我通过了非常基本的方法来测试我的 Excel 工作表数据输出。

这是我正在尝试的代码

else if($request->p['type'] == 'excel')
    {

        $report_type_name = "Graph Survey Report";
        $ExcelReport = new ExcelApExport($sections, $group_definition, $questions,    $sample_corrections);
        $objPHPExcel = $ExcelReport->export($sets, $disp_filter);
        header('Content-Type: application/vnd.ms-excel');

        header("Content-Disposition: attachment; "
            . escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xls"));
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

        $objWriter->save('php://output');

        exit;
    }

我也在这里创建对象并在这里调用phpexcel方法

public function export($Sets, $disp_filter)
 {

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()->setTitle("Offic excel Test Document");
    $objPHPExcel->getProperties()->setSubject(" Test Document");
    $objPHPExcel->getProperties()->setDescription("Test document for  XLS, generated using PHP     classes.");
    //echo date('H:i:s') . " Add some data\n";
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
     $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
    $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
   $objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

    return $objPHPExcel;

}

请您告诉我为什么这些垃圾数据显示在我的文件中,而不是预期的数据。 提前致谢

【问题讨论】:

  • 几乎可以肯定,因为在将文件保存到 php://output
  • 我没有得到我要添加任何空格的代码部分,你能建议我在哪里可以在代码中修复它
  • 我不知道....除了您发布的两个小方法之外,我看不到您的代码....如果您必须这样做,请尝试执行 ob_clean()在保存()之前
  • 太棒了!!!!那行得通……天哪,除了这件小事,我什么都试过了……:P非常感谢:) :)
  • 请注意,使用 ob_clean() 确实是一种解决方法;在脚本中找到实际的换行符应该是更好的解决方案,但我很感激这并不总是那么容易

标签: php phpexcel


【解决方案1】:

基本需要

ob_clean();

在保存对象之前。

【讨论】:

    【解决方案2】:
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    

    这是 .xlsx 文件的编写器。 (见:https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xlsx.php)

    所以正确的标题应该是:

    // Redirect output to a client’s web browser (Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header("Content-Disposition: attachment; "
            . escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xlsx")); //.xlsX!
    

    旧 .xls 文件的编写者是:

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    

    见:https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xls.php

    【讨论】:

    • 我尝试了这两组:1. 在一个标题中使用 xlsx 扩展名,在 createWriter 中使用 Excel2007,这给了我无法在 Excel 中打开的 xlsx 文件,我通过文本编辑器打开它再次拥有所有写在里面的废话不是我所期望的。 2. 我还尝试了一组 Excel5 和 xls 扩展,这给了我上面的结果。
    【解决方案3】:

    我最近遇到了这个问题,在这里发现了另一个帖子,如果您在使用 ob_clean() 后仍然遇到问题,这将非常有帮助

    check this post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2017-08-23
      • 2019-06-04
      相关资源
      最近更新 更多