【问题标题】:Phpexcel cant download excel file with FirefoxPhpexcel 无法使用 Firefox 下载 excel 文件
【发布时间】:2016-04-22 18:53:58
【问题描述】:

我正在使用 PHPExcel 库在我的项目中生成一个 excel 文件,当我使用 Chrome 时工作正常,但是当我使用 Firefox 时它不起作用,我看到了这个问题,它类似于发生的事情我,但没有解决我的问题。

这是我的代码:

  public function export_excel($id){

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()->setCreator("Builder to do")
        ->setTitle("Export data")
        ->setSubject("Fases")
        ->setCategory("Test data");
    $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial');
    $objPHPExcel->getDefaultStyle()->getFont()->setSize(10);

    $title_style = array('font'=> array('bold'=> true,'color' => array('rgb' => 'FF0000'),'size'  => 15,
        'name'  => 'Arial'),'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,));
    $headers_styles =array('font'=> array('bold'=> true,'size'  => 11,'name'  => 'Arial'),'fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,
        'color' => array('rgb' => 'a0f4e6')
    ));


  /*---My stuff---*/


    $highColumn =  $objPHPExcel->getActiveSheet()->getHighestColumn();
    $objPHPExcel->getActiveSheet()->mergeCells('A1:'.'B2');
    $objPHPExcel->getActiveSheet()->mergeCells('A3:'.'C4');
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A3', 'RESERVAS');
    $objPHPExcel->getActiveSheet()->getStyle('A3:'.'C4')->applyFromArray($title_style);
    $objPHPExcel->getActiveSheet()->getStyle('A5:'.$highColumn.'5')->applyFromArray($headers_styles);

    PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
    foreach(range('A',$highColumn) as $colID) {
        $objPHPExcel->getActiveSheet()->getColumnDimension($colID)->setAutoSize(true);
    }

    $activeSheet = $objPHPExcel->getActiveSheet();
    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setName('Sample_image')
        ->setDescription('Sample_image')
        ->setpath('outputfiles/img.jpg')
        ->setWidth(35)
        ->setHeight(30)
        ->setCoordinates('A1');
    $objDrawing->setWorksheet($activeSheet);
    $activeSheet->getColumnDimension('A')->setWidth(15);
    $activeSheet->getRowDimension(1)->setRowHeight(20);
    $objDrawing->setOffsetX(10)->setOffsetY(10);

    $objPHPExcel->getActiveSheet()->setTitle('Listado');
    $objPHPExcel->setActiveSheetIndex(0);



    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
    $objWriter->save('outputfiles/Listado.xlsx');
   // header('Content-Type: application/vnd.ms-excel');
  //  header('Content-Disposition: attachment;filename="Listado.xlsx"');
   // header('Cache-Control: max-age=0');
    echo 'Hello';
   // $objWriter->save('php://output');
    $url = Router::url('/outputfiles/', true).'Listado.xlsx';
    $this->set(array('url' =>$url,'_serialize' => array('url')));
}

【问题讨论】:

  • 如果你删除最后 4 行除了 $objWriter->save('php://output'); ???我有一个工作的代码,之后你不需要路由任何东西,我想......看起来你正试图将它保存到磁盘并从创建的文件中下载它,对吗??
  • 带有注释的行完美运行,我可以使用 Chrome 在我的下载文件中看到 excel 文件,但是当我使用 Firefox 时,我什么也看不到
  • 检查我的答案...我刚刚测试了在 chrome 和 firefox 中下载它们的代码,它工作得很好..
  • echo 'Hello'; 将文件发送到任何浏览器都会导致问题
  • 是的,我意识到并且已经删除了

标签: php phpexcel


【解决方案1】:

如果您只想下载(不保存在服务器中),请尝试:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Listado.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');

但是如果你想保存它然后到服务器然后下载它尝试:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('outputfiles/Listado.xlsx');
header('outputfiles/Listado.xlsx');

【讨论】:

  • 还是不行,我需要通过缺陷将excel文件下载到我的下载文件中
  • 我在生产中就有这样的代码,我刚刚安装了一个新的 firefox 实例,只是为了检查它,它可以工作。如果你重置 firefox 上的默认设置怎么办?您是否尝试过其他 Firefox 实例?你有它在公共可访问的服务器上,所以我可以尝试下载它吗??
  • 实际上 XLSX 的正确 MIME 类型是 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • 使用正确的 MIME 类型,如 @pilsetnieks 所述。如果您仍然看到问题,则可能是 Firefox 的问题。您可以在此处找到更多详细信息:github.com/box/spout/issues/197
  • 我尝试了@pilsetnieks 的建议但仍然无法正常工作,当我添加 'header('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');'在 Chrome 中也不能在 FireFox 中使用
【解决方案2】:

我终于可以解决问题了,浏览器 Chrome 和 Firefox 都在服务器上完成了这项工作,但只有 Chrome 导出了 excel 文件,所以我在客户端替换了这行

  Restangular.all('todos').one('toExprotExcel', $rootScope.client.currentWork.Work.id).customPOST({
                        ids: list,
                        fields: listFields
                    }).then(function (data) {
                        $scope.loadingExcel = false;
                        link.href = data.url;
                       link.download = "Listado.xlsx";
                       link.click();

                    }, function () {
                        $scope.loadingExcel = false;
                    });
                }
                $scope.loadingExcel = false;
            });

为此

 Restangular.all('todos').one('toExprotExcel', $rootScope.client.currentWork.Work.id).customPOST({
                        ids: list,
                        fields: listFields
                    }).then(function (data) {
                        $scope.loadingExcel = false;

                        window.location.href = data.url;

                    }, function () {
                        $scope.loadingExcel = false;
                    });
                }
                $scope.loadingExcel = false;
            });

这在服务器中

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('outputfiles/Listado.xlsx');
$url = Router::url('/outputfiles/', true).'Listado.xlsx';
$this->set(array('url' =>$url,'_serialize' => array('url')));

工作正常!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多