views:

128

answers:

3

I am generating an HTML table full of data. They need it to be an editable spreadsheet though that they can save and edit.

I currently have it exactly as they want but as an HTML table, is there anyway I can convert this to an excel spread sheet that they can download?

Thanks!!

+1  A: 

Output the same data as Comma Separated Values (CSV). Most spreadsheet applications will recognize this.

C. Ross
+3  A: 

You could use Spreadsheet_Excel_Writer PEAR package to achive a downloadable file as output.

erenon
+1: Do it properly and generate an Excel file; don't rely on Excel forever handling HTML-served-as-Excel
Ken Keenan
+3  A: 

Here's what I use, hasn't failed me yet:

 header('Cache-Control: no-store, no-cache, must-revalidate');     // HTTP/1.1
 header('Cache-Control: pre-check=0, post-check=0, max-age=0');    // HTTP/1.1
 header ("Pragma: no-cache");
 header("Expires: 0");
 header('Content-Transfer-Encoding: none');
 header('Content-Type: application/vnd.ms-excel;');                 // This should work for IE & Opera
 header("Content-type: application/x-msexcel");                     // This should work for the rest
 header('Content-Disposition: attachment; filename="'.basename('yourFilenameHere.xls').'"');

'yourFilenameHere.xls' should obviously be changed :)

Arms
This is one of the simplest and most convenient ways to do this. Excel launches directly from the browser (doesn't need to save a CDF, open Excel, find CDF and import it)... there's an assumption on Excel here of course, but beyond that it works very well. (Note - you may want the cache setting set to "cache" IIRC, IE6 has issues whereby it tries to save the "excel" file to the temp files, then load Excel... then try to load the file from the temp files (which it already deleted) - if anyone knows the exact KB article, please post.
scunliffe
I use similar for word as well. works great!
corymathews
I tried this and it does let me save it as an Excel file :) ...however when trying to open the excel file I am told it is formatted wrong. So I removed all the HTML tags and made it pure TSV and save it as excel that way. Still it does save as an excel file but then tells me its formated wrong when trying to open it?
John Isaacks
One thing I found out: make sure "ID" isn't the first cell.
ceejayoz
I figured out why it wasn't working, I was using \n but I needed to use \r\n
John Isaacks