PHP Generate CSV Cell are not displaying on windows - php

I am using Cakephp 2.0. I need to generate a Report on daily basics in an excel format.
So i written a function to generate a CSV File. Its working fine in Linux machine when i downloads the file in OpenOffice but if I try to open the same file in windows Microsoft then there is no cells, but the data is displaying properly. With this I listed my coding
$model.= "-".date('d M,Y');
header ("Expires: Mon, ".date('d M,Y')." GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/vnd.ms-excel");
header ("Content-Disposition: attachment; filename=\"$model.xls" );
header ("Content-Description: Generated Report" );
Any one helps thanx

I think you might doing something wrong here Please check your header. You have mentioned csv in the question and you're writing excel header. That is might be confusing MS Excel
header ("Content-type: application/vnd.ms-excel");
header ("Content-Disposition: attachment; filename=\"$model.xls" );
header ("Content-Description: Generated Report" );
I'm using this kind of header format which is perfectly working for me in windows and as well as in Open office. Please give a try with this.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: octet/stream");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-disposition: attachment; filename=" . basename($filename.".csv"));
header("refresh: 0; url= ".$redirect_url);

Excel CSV files should be named SSV files, because the data columns need to be separated by a semicolon instead of a comma. Also the data should be enclosed in quotes and any inner quotes need to be escaped as double quote; e.g. 1;"Some ""string"" with escapes".

Related

export file from table to excel using php

hi im a newbie in programming and im using php. below is my code for .xls. i have one form where i have three buttons the add search and "save to excel". and also i have a table where my output for add button will be shown and that was connected to my database. now my problem is when i click the button "save to excel", the whole data in my form appear to the excel. what i want is that the table in my form will be only save in my excel. not the whole data in my form. if i can only post the image here you probably might understand me clearly. :( . but please i need some help. :( .. thanks
if (isset($_POST['download']))
{
$file="document_name" . date('Ymd') . ".xls";
header("Content-type: application/vnd.ms-excel");// file extension name
header("Content-Disposition: attachment; filename=$file");
}
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=line_utilization_report.xls ");
header("Content-Transfer-Encoding: binary ");

PHP: Forcing a download not working

I have the following code to push a zip file for download.
$filename = "ResourcePack_".time().".zip";
$destination = $basepath."downloads/$filename";
if($this->createdownload($files,$destination,false)){
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$destination").";");
header("Content-Disposition: attachment; filename='$filename'");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
ob_end_flush();
#readfile($destination);
if(file_exists($destination)){
unlink($destination);
}
}
I know the createdownload function is working to generate the zip file just fine because I see the file being created on the server. The problem is file is being written to the browser as a bunch of garbage instead of opening a download stream. Am I missing something in my headers?
EDIT
I was right. My problem is not with the php, but that calling the php file that generates this code via a JQuery $.ajax call is the problem. Using $.ajax automatically sets the Accept-Encoding request header to values incompatible with zip files. So, intead of using $.ajax I just used a simple window.open javascript command to call the same php page and it works just fine with the headers.
try to put a die after the #readfile
and remove the #, to see if you have any other error related with the file reading.
i have some code doing the same thing and this works for me:
header("Pragma: public");
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-Disposition: inline; filename="' . $filename . '"');
header('Content-type: application/zip');
//header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($destination));
readfile($destination);
die();
try passing proper type for that file. I think its fileinfo mime type see http://www.php.net/manual/en/ref.fileinfo.php
header("Content-Type: $file_type");
Also you have semicolon after octet-stream remove it
header("Content-type: application/octet-stream");

PHPExcel set specific headers for file format

While googling I found two different sets of headers that need to be set when outputting excel generated in different file format.
for e.g.
For Type "Excel5" headers are:
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 ");
For Type "Excel2007" headers are:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');
My question: is there need to set up different headers for each file type as there are other file types also CSV, HTML and PDF?
header("Pragma: public");
No - this is just wrong - though lots of people think it has something to do with caching
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
Nothing to do with Excel - these just control caching
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
No - there should only be one content-type header. For a MS Excel file using OLE, the mimetype should be application/vnd.ms-excel
Only the second header above is a valid mime type.
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");
The second header is redundant, the former specifies a filename for the download.
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
Only for a .xlsx file (i.e. saved in XML). Otherwise you should use application/vnd.ms-excel. Indeed the latter should be backwardly compatible.
My question: is there need to set up different headers for each file type
Yes - the Content-Type header is the file type. But only this header needs to change.
C.

How do I get csv file to download on IE? Works on firefox

I'm struggling with an odd error. I have a simple web app that grabs stuff from a DB then outputs it as a downloadable csv file. It works on firefox and chrome, but IE fails to recognize it as a csv file (thinking it is a html fle) and when I click save I get the error, "Unable to download {name of file} from {name of site}. Unable to open this internet site. ..."
Code:
session_start();
//some logic goes here...
//generate csv header
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=exportevent.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "Event: " . $event_title . "\n";
//print the column names
echo "Last Name, First Name, Company \n";
while($row = mysql_fetch_assoc($result))
{
echo $row['atlname'] . ',' . $row['atfname'] . ',' . $row['atcompany'] . "\n";
}
I've played around with the content-type a whole bunch, but that had no effect.
Update: I've tried text/csv, application/vnd.ms-excel (and variations of this), text/plain, and some others that I now forget with no luck.
This is IE8 btw.
Update 2: The connection is over SSL.
Don't we love IE? :)
Try using those headers:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"exportevent.csv\";" );
header("Content-Transfer-Encoding: binary");
I think that the octet-stream content type forces IE to download the file.
We recently ran into this problem ourselves. See this MSKB article
These are the headers we ended up having to use to get it to work over SSL.
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file_name\";");
header("Content-length: " . strlen($csv_string));
I've had success with the following:
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=File.csv");
Setting the type to application/vnd.ms-excel seemed to do the trick in my case. This is all in a file that is opened by submitting a form using
target="_blank"
The only extra code I had to add for IE to work with SSL was: header("Pragma: public");
So my headers look like this now:
header("Pragma: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=some_filename.csv");
We have just had the same issue and after adding many headers and getting a working link I then removed them one by one and found the key one for us was
"Cache-Control: public"
so in the end we just had
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=some_filename.csv");
which worked fine.
Try setting your content type to text/csv instead of application/octet-stream.
Since application/octet-stream is a generic binary mime type (and doesn't match the '.csv' extension), Internet explorer might be ignoring it and computing the mime type based on the file extension.
After using Javascript it will solve your problem.
Use this for IE,
var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
For more information i have written tutorial on that,
see - Download JSON data in CSV format Cross Browser Support
Hope this will be helpful for you.
The solution for me was:
header_remove();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=brokerlist.csv');
echo $content;
Did you try the Content-type: text/csv ?
Some time ago I've got a problem with IE6 opening pdf files, and crashing when AdobeReader 6.0 was installed and tried to open file in browser window. Than I found somewhere this header:
header('Content-Type: application/force-download');
And it solved the problem, every pdf file was downloaded and opened in Adobe instead of IE.
This simply doesn't make sense. I tried the accepted answer, all the other answers in here, and it didn't work for me. I tried their permutations, and somehow I managed to make it work in IE like so:
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/vnd.ms-exce");
header("Content-Disposition: attachment; filename=coupons.csv" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($csv));
echo $csv;
die();
One thing I did is to empty the cache every freaking time I test the code. And it still doesn't make sense. Just in case someone might need this desperately ;)
If you are trying to accomplish this task (getting a CSV file to download in IE8) using Salesforce.com (in which case your front-end is Visualforce and you can't set all of the headers, only some of them), here's what you need:
<apex:page cache="true"
contentType="application/octet-stream#myAwesomeFileName.csv"
showHeader="false" sidebar="false" standardStylesheets="false">
<apex:outputText value="{!csvContent}" escape="false"/>
</apex:page>
The key pieces here are cache=true, which, in conjunction with the default expires=0 attribute, achieves the following headers:
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
And then the contentType of application/octet-stream --- doing text/csv fails for IE8.

PHP: Turn HTML table into spread sheet?

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.

Categories