8

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.

Tam Vo
  • 175
  • 2
  • 3
  • 10
  • did you try changing `$filename` as `/path/to/save/text.xlsx` ? – Criesto Sep 07 '15 at 08:07
  • You can't control where a user's/client's browser saves the file, not should you be able to.... consider if you tried to save to `/etc/passwd` on the users machine, that would be an incredible security flaw if it was permitted – Mark Baker Mar 12 '19 at 02:18

4 Answers4

10

Change the file name to desired path i.e,

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

It Works For Me...

Vatsal Patel
  • 317
  • 5
  • 15
6
$objWriter->save($filename);

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

$filename = '/path/to/folder/test.xlsx';
$objWriter->save($filename);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • example: I have 1 button when click button show window to choose location save. how do it ? – Tam Vo Sep 07 '15 at 08:11
  • You mean you want to send the file to the user's browser? And let them select what folder they want to save the file in on their PC? – Mark Baker Sep 07 '15 at 08:12
  • 2
    Then send the appropriate headers, and save to `php://output` as shown in `/Examples/01simple-download-xlsx.php` – Mark Baker Sep 07 '15 at 08:57
  • @MarkBaker What will be the headers? I am creating multiple files using while loop how can I save all files in a specific folder. Above code works fine but the file cannot open. – Zaid Bin Khalid Mar 11 '19 at 05:33
  • 1
    @ZaidBinKhalid If you want to save the files to disk, then you provide a filesystem filename to the `save()` method instead of the output stream... that saves to a file on the server which you can zip and then send the zipfile to the browser... but http limits you to one request, one response, you can't send more than one output/file back to the server at a time – Mark Baker Mar 12 '19 at 02:16
  • 1
    @MarkBaker Thank you it is working fine for me. I just pass the absolute path and remove all headers. – Zaid Bin Khalid Mar 12 '19 at 02:18
2

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 ");
Serhat Akay
  • 536
  • 3
  • 10
0

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();
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68