简体   繁体   中英

How to choose location to save file excel with PHPExcel

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.

 $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 = '/path/to/folder/test.xlsx';
$objWriter->save($filename);

If you include below headers to your php file. 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:

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

For .xls files created with the Excel5 Writer:

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

OR

For .xlsx files created with the Excel2007 Writer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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