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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

generate CSV file in browser using PHP?

Status
Not open for further replies.

shadedecho

Programmer
Oct 4, 2002
336
US
I know this is like the most simple type of thing, but I can't seem to make this work.

I want to have a .php file which based on a parameter that is passed to it, it generates a particular set of data in CSV format, and I want to signal for the viewer of this page to have it automatically launch and be viewed in Excel. (the user of this page will always be on a windows machine with microsoft excel 97 or 2000).

so what I have is this:

blah.php:
Code:
<?php
header(&quot;Content-Type: application/vnd.ms-excel&quot;);
header(&quot;Content-Disposition: inline; filename=blah.csv&quot;);

echo &quot;field1,field2,field3&quot;;
echo $param.&quot;,val2,val3&quot;;

exit;
?>

then in the browser, its simply called by:

blah.php?param=val1

So, when I do this with just typing that into a new browser window, I get my data printed out fine, but it doesn't launch Excel to open the data, just prints it in text.

But, if i have a javascript launch a new popup window (which is how my application will do it) with that blah.php?param=val1 as the source file, i get an error that says &quot;Excel could not open the file and then the next error box after it talks about &quot;...could be one of several reasons&quot; and it mentions things like the file/path not existing, or the file being in use, or that the name of the workbook that i'm trying to save is the same as a read-only one.

None of these errors make any sense to me. I think I must not be sending the correct/enough headers to tell Excel how to launch. Can someone please tell me what I need to do?
 
correction, I get that MS Excel error even if it manually type the address into a browser window. Clearly, the MIME type is telling Excel to start loading the document, but something after that fails.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top