Phpexcel无法使用Firefox

时间:2016-04-22 18:53:58

标签: php phpexcel

我正在使用PHPExcel库在我的项目中生成一个excel文件,当我使用Chrome时工作正常,但是当我使用Firefox时它确实无法正常工作,我看到了这个问题,它和&# #39;类似于我发生的事情,但并没有解决我的问题。

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

最后我可以解决问题,浏览器Chrome和Firefox都在服务器上执行此任务,但只有Chrome导出excel文件,因此我在客户端替换此行

newtype

这个

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

并且工作正常!!!