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')));
并且工作正常!!!
我正在使用 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')));
并且工作正常!!!