Jump to content

Export to Excel displaying error, but works...


busnut

Recommended Posts

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"; 
?> 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.