Generate Excel file, but do not save to the server

瘦欲@ 提交于 2019-12-11 02:25:32

问题


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

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

I don't want to save generated file to the server. I just want generate Excel file and give it to user for downloading.

Is it possible to do?

Thanks.


回答1:


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;

instead of

$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



来源:https://stackoverflow.com/questions/20676839/generate-excel-file-but-do-not-save-to-the-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!