busnut Posted May 17, 2009 Share Posted May 17, 2009 I've got a script where users can export information to ms excel, and on one computer that uses XP, it exports fine without any errors, but then on my Vista pc, it comes up with this message: "The file you are trying to open, '<filename.xls>', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trustworthy source before opening the file. Do you want to open the file now? Yes | No | Help" By selecting yes, the file opens fine, but im trying to stop it from displaying that error message, and as mentioned, it only seems to appear on the Vista machine. So is it Vista, or is there something wrong with my code? Below is my code: <?php $localhost = '***'; $username = '***'; $password = '***'; $database = '***'; $table = '***'; $file = '***'; $fields = "busno, chassisbody, depot, rego"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $select = "SELECT $fields FROM $table WHERE active='Y' ORDER BY busno +0 ASC"; $export = mysql_query($select); $count = mysql_num_fields($export); for ($i = 0; $i < $count; $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"; } $filename = $file." ".date("Y-m-d",time()); header("Content-type: application/x-msexcel"); header("Content-Disposition: attachment; filename=".$filename.".xls"); header("Pragma: no-cache"); header("Expires: 0"); print "$header\n$data"; ?> Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted May 17, 2009 Share Posted May 17, 2009 header("Content-Type: application/vnd.ms-excel"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); http://www.evolt.org/node/26896 Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted May 17, 2009 Share Posted May 17, 2009 you should create CSV files they are easy to create and open in excell Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted May 17, 2009 Share Posted May 17, 2009 As nadeemshafi9 has pointed out, the correct headers will probably fix your problem Or you could always create a real Excel file using a library such as PHPExcel, rather than a tab-separated file. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted May 18, 2009 Share Posted May 18, 2009 As nadeemshafi9 has pointed out, the correct headers will probably fix your problem Or you could always create a real Excel file using a library such as PHPExcel, rather than a tab-separated file. is this available on a UNIX platform ? looks like it can, its third party anywyas i remember using the php or PEAR version and it waas restricted to platforms with COM installed wheras this one uses open XML am i right ? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted May 19, 2009 Share Posted May 19, 2009 Or you could always create a real Excel file using a library such as PHPExcel, rather than a tab-separated file. is this available on a UNIX platform ? looks like it can, its third party anywyas i remember using the php or PEAR version and it waas restricted to platforms with COM installed wheras this one uses open XML am i right ? PHPExcel is pure PHP, though it requires the zip, xml and gd2 extensions, but no need for COM, so it will run on Windows, *nix and even Mac. It can read/write either Excel 2007 ("almost open XML") or Excel 5 formats. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.