Exporting data to excel in Wordpress - php

I'm trying to export some data to an excel in my wordpress page, but when I save the file it print it on my browser and never open the dialog to save it.
I'm trying to save with this command.
$objWriter->save('php://output');
Thank you in advance.

Yeah, I instantiate it before.
$objWriter = PHPExcel_IOFactory::createWriter($this->excel->workbook,'Excel5');
And I have use several headers, for example this one:
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
And this too:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

Related

Generate ms word document using PhpWord

I'm going to generate some document using PhpWord, I have some draft document where I set client_name dynamically.
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($draftUrl);
$templateProcessor->setValue("client_name", $clientName);
$filename = '30.docx';
$templateProcessor->saveAs($filename);
then I read this file and user can download it
header('Content-Type: application/octet-stream');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Disposition: attachment; filename='$filename'");
readfile($filename);
when I try to open it via MsOffice I have this message:
when I click OK:
and after accepting this finally I see my document ready. What do I need to generate readable document?
I just added ob_clean() to my code before read and everything works fine.
This function discards the contents of the output buffer.
This function does not destroy the output buffer like ob_end_clean()
does.
so I ensured that undesired output will not get into the generated document.
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($draftUrl);
$templateProcessor->setValue("client_name", $clientName);
$filename = '30.docx';
$templateProcessor->saveAs($filename);
header('Content-Type: application/octet-stream');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Disposition: attachment; filename='$filename'");
ob_clean();
readfile($filename);

exporting csv to iOS 9 safari .. unable to read document

I'm using php to export a csv to mobile safari. I'm getting this error when I try to download the csv.
Does anyone have any ideas?
ini_set('memory_limit', '2048M');
header("Cache-Control: must-revalidate");
header("Pragma: must-revalidate");
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=Activity.csv");
header("Expires: 0");
i changed content-type to this.. solved it
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

Download Excel From Server Using PHP

Can anyone help me to write code on PHP to download Excel File from the server.
I have created below codes using header and readfile but the downloaded file was corrupted.
//content type
header('Content-type: application/vnd.ms-excel');
//open/save dialog box
header('Content-Disposition: attachment; filename='.$fileName);
//read from server and write to buffer
readfile($reportPath);
Can anyone help me on the best way to download existing Excel file from the server.
Please see below image of data after downloaded
ob_clean();
put that code before all header declaration
I suggest to use the X-SENDFILE header. https://tn123.org/mod_xsendfile/
I use something like the following:
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Length: ".filesize($path));
readfile($path);
exit;
Make sure you're not outputting anything before or after the readfile.

Error while setting the path for download headers of an Excel file

I have the following code:
$objWriter = new PHPExcel_Writer_Excel2007($excel);
$objWriter->save("uploads/".$excel_file);
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="uploads/'.$excel_file.'"');
header("Pragma: no-cache");
header("Expires: 0");
The file is indeed in the "uploads" directory, but I fail to link it correctly in this line of code: header('Content-Disposition: attachment; filename="uploads/'.$excel_file.'"');
Any idea about how should I set the path in order to download the correct file?
Thanks.
Replace that line with
header("Content-Disposition: attachment; filename=uploads/".$excel_file.";");
And at last, add following lines
readfile($excel_file);
exit;

Download with php with headers do not work on IE8

I am trying to download a excel file generated on the fly with php headers:
$filename = "assets.xls";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
But this does not work on IE8 (but on some other pc with IE8 works???!!). IE8 tries to download the export.php file instead of assets.xls. Any idea why IE8 do this?
Try to format the header correctly as per the HTTP spec with a space between ; and filename and quotes around the filename:
header('Content-Disposition: attachment; filename="' . $filename . '"');
I have encountered the same problem. And I use the followed method to fix the problem.
header("Cache-Control: private");
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=$filename");
I am having a similar issue. I have added the following header before the Content-Deposition header.
header("Content-type: text/csv");
header("X-Download-Options: noopen");
header("Content-Disposition: attachment; filename=\"ExcelFileName.csv\"");
It seems to work for me. However, you have to save the file first. You cannot open right away.
I have exact same problem! Just got it to work by removing the 'Content-Type' header, so I guess IE8 doesn't play well with that type..? Not sure yet what the best alternative is, but that definitely is the player for me.
Try this:
$filename = 'Excel_Sheet_'.date('Ymd').".xls";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/vnd.ms-excel; ");
header("Content-Transfer-Encoding: binary");
header('Cache-Control: max-age=0');
ob_clean();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
After trying to fight with a similar issue for a whole afternoon I discovered that setting
header("Cache-Control: private");
Was the best solution.
I had already tried ensuring the Content-Length, Content-Type and Content-Disposition were set and correctly formatted.
The issue is actually that new IE8 windows and tabs do not seem to like a download sent through PHP headers the first time it occurs.
When retrying the file after an initial attempt it works fine (in my cases).
After setting the Cache-Control as mentioned above all my links have worked without a problem in IE8.

Categories