Phpexcel无法用Firefox下载excel文件


Phpexcel cant download excel file with Firefox

我正在使用 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')));
}

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

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

最后我可以解决这个问题,浏览器 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')));

并且工作正常!!