我已经使用phpexcel创建了一个excel文件并成功将其保存在服务器上。但我真正想做的是说浏览器即时下载这个文件。对此有什么解决方法吗? 这是我的代码:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$col = 0;
foreach ($json[0] as $key => $value) {
$a = $key;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $key);
$col++;
}
$row = 2;
foreach ($json as $itemGroup) {
$col = 0;
foreach ($itemGroup as $key => $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
}
$objPHPExcel->getActiveSheet()->setTitle('Report');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
try {
$fileName = date("Y-m-d H:i:s");
$objWriter->save('/uploaded_images/generatedReports/' . $fileName . '.xlsx');
return true;
} catch (Exception $exc) {
return false;
}
答案 0 :(得分:1)
查看示例01simple-download-xlsx.php,其中显示了要设置的标头,您应该保存到php://输出而不是“命名”文件
答案 1 :(得分:0)
header('Location : /uploaded_images/generatedReports/' . $fileName . '.xlsx');
答案 2 :(得分:0)
尝试使用附件标题?
header("Content-Disposition: attachment; filename=your_file.xls");
答案 3 :(得分:0)
Berofe returning true in try block you have to use header method for downloadable action
$filename ="excelreport.xls";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo "your content \t you next tabcontent \n your next line content \t"
答案 4 :(得分:0)
您可以设置相应的标题,然后使用' php:// stdout'作为输出文件名。
e.g。
$filename = date("Y-m-d H:i:s").'.xls';
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
$objWriter->save('php://stdout');
return true;