5

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!!

Adriano Varoli Piazza
  • 7,297
  • 5
  • 39
  • 50
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422

3 Answers3

5

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 :)

Steven Mercatante
  • 24,757
  • 9
  • 65
  • 109
  • 1
    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 Oct 15 '09 at 17:10
  • 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? – JD Isaacks Oct 15 '09 at 17:16
  • 1
    One thing I found out: make sure "ID" isn't the first cell. – ceejayoz Oct 15 '09 at 17:27
  • I figured out why it wasn't working, I was using \n but I needed to use \r\n – JD Isaacks Oct 15 '09 at 18:27
4

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

erenon
  • 18,838
  • 2
  • 61
  • 93
0

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

C. Ross
  • 31,137
  • 42
  • 147
  • 238