Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically opening an excel downloaded file

Status
Not open for further replies.

tweenerz

Programmer
Mar 25, 2002
202
US
I am attempting to create an excel file through php and dynamically open it in excel. But I want to download and prompt the user to either open it or save it. The prompt appears and it saves to the desktop if the user selects that option.

But the problem lies in the 'Open' option. When the user clicks 'Open', it opens excel, and then an error comes up stating that the file cannot be found (it is looking in the IE temp directory for the file). This is what I have:

Code:
header( "Content-type: application/vnd.ms-excel\r\n" );
header( "Content-Disposition: attachment; filename=\"bob.csv\"" );

Ive tried quotes, no quotes, line breaks, etc., but nothing seems to work. I don't want to get rid of the attachment option because then I will be redirected to another page.

If excel is already open, there is no error. It only occurs when excel has to be opened prior to opening the downloaded file.

Any ideas?
 

That's a browser/OS configuration issue. What might work in IE may not work in Mozilla or Firefox.

Luckily, there are probably enough people here with sufficient experience that they'll be able to help you, even though you're off topic, otherwise I'd direct you to a Windoze forum.
 
when I generate CSV files on the fly from mysql, I use the following headers:

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=".$file );
header('Pragma: no-cache');
header('Expires: 0');

(or if you like, the whole code)
Code:
<?
// check this script has been passed a $_POST[sql] variable
// *note - it also needs a $_POST[filename] and $_POST[db]
if(!isset($_POST[sql])){
  echo "No Query";
  exit;
	}else{
	$sql=stripslashes($_POST[sql]);
	$file=$_POST[filename].".csv";
}

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=".$file );
header('Pragma: no-cache');
header('Expires: 0');

// set up the connection
require('config.inc.php');

$db=$_POST[db];

mysql_select_db($db,$connection);

$result = mysql_query($sql,$connection);

if($result){

mysql_checkerror();

	$columns=@mysql_num_fields($result);
for ($i = 0; $i < mysql_num_fields($result); $i++) {

	print "\"".mysql_field_name($result,$i)."\",";

}
echo "\n";

while ($myrow = mysql_fetch_array($result)){
	for ($i = 0; $i < ($columns); $i++) {
		echo "\"".$myrow[$i]."\",";
	}
		echo "\n";
}
}else{
echo "No results";
}
////////////////////////////////////
function mysql_checkerror(){

	$err_no=mysql_errno();

	if ($err_no > 0 ){

			echo "<h2> Error:</h1> " . mysql_errno() . ": " . mysql_error() . "<br>\n";

		exit;

	}
}
?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks for the replies.

I found out what was causing the problem. I was starting a session prior to displaying those headers and that was what was causing Excel to choke. Don't know why and I haven't done any investigation but would welcome any ideas. I guess the session may have been cluttering the headers and disrupting whatver Excel needed to read prior to opening the file.

As soon as I commented out session_start(), everything worked fine.

Im not sure what no-cahce does or if it would have helped, but I will look into it as well.

This is a new one on me!
 
Have a search on th microsoft knowledge base, they have loads of odd things with http headers, some they say is by design !. Search this forum it has been posted
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top