I'm working on a Yii2 rest Api that's connected to AngularJS front end.
I'm trying to build an actionDownloadAsExcel method that can help me download a specific model. What would be a good way to do this?
I installed "phpOffice\phpExcel" but I don't really know how to use it for my purpose.
You can find all the data you want to save to the excel, print that using an html table and setting a proper excel header before the view is rendered:
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
Source:
PHP Excel Header
Related
I am working on the reporting part of my project, the project contain hundreds of dynamic generated reports and each report contain images and formatted followed by Table. And all these reports need to be downloadable in doc/docx (Ms Word), xls /xlsx(MS Excel) and PDF
For this I want to Convert the Generated HTML report to the corresponding file
for this I am using following code
function export_attendance_view_doc($htmlcontent){
header("Content-Type: application/vnd.msword");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=attendance_report.doc");
echo $htmlcontent;
}
function export_attendance_view_xls($htmlcontent){
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=attendance_report.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=attendance_report.doc");
echo $htmlcontent;
}
Using the above code I am getting the doc and xls file but the issue is that the image is not rendering in the file if the script is run in live server working fine in localhost if I am using full url in <img src not displaying image in local if using relative path. Same in the case of Excel file.
When i using link download is result download OK
But when I using form submit is error
This is code:
download
in download.php i using code:
...
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/vnd.android.package-archive");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='test.apk';" );
readfile('test.apk');
...
When click on download is from test.apk auto convert to test.zip (I using android 2.3), How to fix it?
This error is occured for mime type :
to rectify this error add this code in .htaccess
AddType application/vnd.android.package-archive
I am building a web application using Yii. The application generates MS-Word documents. Does anyone can give me a hint on how to force the browser to open a save as dialog.
Thanks
You will need to send these headers:
header("Pragma: no-cache"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=\"test.doc\"");
header("Content-Transfer-Encoding: binary");
This sends the file as test.doc download.
I want to use the ajax functionality to download whereby the user will click the download link which will (using ajax and $_GET) access a PHP file which will process the sent $_GET variables and access the correct file for downloading.
I have a few PHP scripts to handle the processing of the $_GET variables which work on their own but when accessed using Ajax, they stop working.
The Ajax/PHP code im using is below:
function ajaxDown(){
$('#downloadmsg').html(
'<img src=\"media/images/ajaxloader.gif\" width=\"128\" height=\"15\">');
$('#downloadmsg').load(
'media/downloads/downManager.php?file=".$filequery['filename']."&ftype=".$downex[1]."');
}
Please look through my code and help me find what Im doing wrong.
Thanx
I think the problem is that you're trying to load a file result INTO #downloadmsg, which isn't going to work because .load() is only going to load results as HTML...NOT binary data or other encoding.
One approach that might work is creating a hidden iframe in HTML, like this:
<iframe id="secretIFrame" src="" style="display:none; visibility:hidden;"></iframe>
Then, set the attr of the iframe to your querystring:
$("#secretIFrame").attr("src","myphpscript.php?option1=apple&option2=orange");
and then using PHP headers to force the download when the source is set (here's an example of an exporter header set from one of my scripts that uses an octet stream):
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=data.xls ");
header("Content-Transfer-Encoding: binary ");
Hope this helps!
I know I'm late! But I think I have a solution that's a little cleaner without the use of a hidden iframe and you won't even need an ajax request to do it! Using PHP Headers as noted in the accepted answer in a download.php file
<?php
//download.php
/*
All your verification code will go here
*/
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=".$_GET['file']);
header("Content-Transfer-Encoding: binary ");
And on the JS end, simply
function download(filename){
window.location="http://whateveryoursiteis.com/download.php?file="+filename;
}
Works like a charm :O
I am generating an HTML table full of data. They need it to be an editable spreadsheet though that they can save and edit.
I currently have it exactly as they want but as an HTML table, is there anyway I can convert this to an excel spread sheet that they can download?
Thanks!!
Here's what I use, hasn't failed me yet:
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
header ("Pragma: no-cache");
header("Expires: 0");
header('Content-Transfer-Encoding: none');
header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera
header("Content-type: application/x-msexcel"); // This should work for the rest
header('Content-Disposition: attachment; filename="'.basename('yourFilenameHere.xls').'"');
'yourFilenameHere.xls' should obviously be changed :)
You could use Spreadsheet_Excel_Writer PEAR package to achive a downloadable file as output.
Output the same data as Comma Separated Values (CSV). Most spreadsheet applications will recognize this.