我已经用 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;
}