php 如何使用PHPExcel选择保存文件excel的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32433750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:58:24  来源:igfitidea点击:

How to choose location to save file excel with PHPExcel

phpyiiphpexcel

提问by Tam Vo

I have a problem when using PHPExcel to create a excel file. I want to choose location to save file excel but I don't know how do it.

使用 PHPExcel 创建 excel 文件时遇到问题。我想选择保存excel文件的位置,但我不知道怎么做。

 $model = new User();
 $labels = $model->attributeNames();
 $data = $model->findAll();
 $objPHPExcel = Yii::app()->excel;

........
$filename = 'text.xlsx';
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($filename);

Please help me. thank you so much.

请帮我。太感谢了。

采纳答案by Serhat Akay

If you include below headers to your php file. Your users will have a download option pop-up:

如果您在 php 文件中包含以下标题。您的用户将有一个下载选项弹出窗口:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");

回答by Vatsal Patel

Change the file name to desired path i.e,

将文件名更改为所需的路径,即,

$name = '/path/to/folder/xyz.xlsx';
$objWriter->save($name);

It Works For Me...

这个对我有用...

回答by Mark Baker

$objWriter->save($filename);

.... change the value of $filenameto be the filepath for wherever you want to save the file, e.g.

.... 将 的值更改$filename为要保存文件的位置的文件路径,例如

$filename = '/path/to/folder/test.xlsx';
$objWriter->save($filename);

回答by pala dattadri

TO DOWNLOAD EXCEL WITH PHPExcel:

使用 PHPExcel 下载 Excel:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 

For .xls files created with the Excel5 Writer:

对于使用 Excel5 Writer 创建的 .xls 文件:

header('Content-Type: application/vnd.ms-excel'); //mime type

OR

或者

For .xlsx files created with the Excel2007 Writer:

对于使用 Excel2007 Writer 创建的 .xlsx 文件:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //mime type

header('Content-Disposition: attachment;filename="you-file-name.xlsx"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache 
ob_end_clean();
$objWriter->save('php://output'`enter code here`);
exit();