生成Excel文件,但不保存到服务器
问题描述:
我有一个Excel文件 file.xlsx
.我使用PHPExcel打开此文件并插入数据:
I have an Excel file file.xlsx
. I open this file with PHPExcel and insert my datas:
$excel2->getActiveSheet()
->setCellValue('A2', $rowCardCode['podpodrazd'].", ".$rowCardCode['schet'])
->setCellValue('C2', $rowCardCode['tovar_name'])
->setCellValue('C3', $rowCardCode['edinica_izmer']);
之后,我可以用新名称(temp/New_file.xlsx
)保存生成的文件.
After, I can save generated file with new name (temp/New_file.xlsx
).
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('files/temp_file/card/card_'.time().'.xlsx');
我不想将生成的文件保存到服务器.我只想生成Excel文件并将其提供给用户进行下载.
有可能吗?
谢谢.
答
PHPExcel/Examples文件夹中的01simple-download-xlsx.php
示例准确显示了如何执行此操作
The 01simple-download-xlsx.php
example in the PHPExcel /Examples folder shows exactly how to do this
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('php://output');
exit;
代替
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('files/temp_file/card/card_'.time().'.xlsx');
但是请确保您没有向浏览器输出任何其他信息
But make sure that you're not outputting anything else at all to the browser