Click to See Complete Forum and Search --> : Trouble Opening PHP-Created XLS File
jdc44
09-30-2005, 01:24 PM
Hey all,
I have a script which creates an Excel file. This file will open in Mozilla but not in IE. I'm thinking it has to do with my header info. Maybe you can shed some light on this for me.
This the header info I am using: (??)
//header info for browser: determines file type ('.doc' or '.xls')
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=CHRToolData.xls");
header("Pragma: no-cache");
header("Expires: 0");
I'm creating tab delimited values ( headers and rows - values ).
$headers .= $values ."\t"
$rowData .= $rowValues ."\t"
$outPut = $headers . $rowData;
echo $outPut;
Again, it works in Mozilla.
Thanks for any help.
I end each row with a . "\n"
dugindog
09-30-2005, 01:38 PM
This may help you. it gathers from a table, then generates a cvs file to be saved.
// database info
$dbname = "database"; //database to connect to
$dbtable = "table";
//edit this line to your server, username, password
$link = @mysql_connect('server','username','password') or die("Unable to connect to database");
//do NOT edit anything below this line!!!!
$db_selected = @mysql_select_db($dbname,$link) or die("Unable to Select database");
$select = "SELECT * FROM " . $table . "'";
$export = @mysql_query($select);
$fields = mysql_num_fields($export);
//echo "test1<BR>";
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
/*
if ($data == "") {
$data = "\n(0) Records Found!\n";
*/
header("Content-type: application/x-msdownload");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=form.csv");
header("Content-Encoding: SomethingIEDoesntKnow");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
jdc44
09-30-2005, 02:03 PM
Thanks,
Your solution helps with this application.
Curious. I have several scripts that create excel files as I tried with this one. Same headers and same style of creating headers and rows. They all work fine, but this one doesn't.
One big difference is the size. The file I am creating in this script is larger than the others: over 1,000 rows and 158 columns each row. Perhaps ???
thanks again,
jdc44
jdc44
09-30-2005, 03:33 PM
It is not the size of my file. It is the content-type: application header...
Side Note, I'm trying this with IE 6.0.2
I have narrowed my problem down to this ( many thanks to dugindog who got me thinking along these lines )
header("Content-Type: application/x-msdownload"); // works but doesn't open file in excel
header("Content-type: application/octet-stream"); // works but the same as above
header("Content-type: application/excel"); // listed on many sites but doesn't work in IE
header("Content-type: application/msexcel"); // also listed on sites but doesn't play nice with IE.
I've sent an email to the main users to tell them how to open the file ( the one using ...x-msdownload"), save as a text file, and then open with excel, and save as xls.
This is a pain for our users. Does anyone know of another application/type for excel that I might try?
Many thanks.
Weedpacket
09-30-2005, 08:09 PM
The official type (as mandated by Microsoft (http://www.microsoft.com/technet/prodtechnol/windows2000serv/technologies/iis/maintain/featusability/mimeiis.mspx) and the IANA (http://www.iana.org/assignments/media-types/application/)) is application/vnd.ms-excel
There are some suggestions and advice in the user notes for the header function about how to smack IE hard enough to get it to behave.
PHP Builder
Copyright Internet.com Inc. All Rights Reserved.