简体   繁体   English

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

[英]How to choose location to save file excel with PHPExcel

I have a problem when using PHPExcel to create a excel file.使用 PHPExcel 创建 excel 文件时遇到问题。 I want to choose location to save file excel but I don't know how do it.我想选择保存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.太感谢了。

Change the file name to desired path ie,将文件名更改为所需的路径,即,

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

It Works For Me...这个对我有用...

$objWriter->save($filename);

.... change the value of $filename to be the filepath for wherever you want to save the file, eg .... 将$filename的值更改为要保存文件的位置的文件路径,例如

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

If you include below headers to your php file.如果您在 php 文件中包含以下标题。 Your users will have a download option pop-up:您的用户将有一个下载选项弹出窗口:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM