0

Possible Duplicate:
How can I export tables to excel from a webpage

EDIT: Just to clarify, it does not HAVE to be an Excel file, but it has to be a spreadsheet file which is able to be opened/edited in Excel.. So whatever would accomplish this most easily is the answer I seek :)

I need to convert an HTML table to some sort of downloadable spreadsheet (preferably for Office Excel). I can do this in jQuery, but would prefer to do it in plain JavaScript. I've searched around and have found a lot of info about doing the reverse (spreadsheet->html), but I need to create a downloadble spreadsheet file of a dynamically built table. Any points for how I could accomplish this would be much appreciated! :)

Community
  • 1
  • 1
tdc
  • 5,174
  • 12
  • 53
  • 102

4 Answers4

2

Don't do this in JavaScript. You need to do this in a server-side scripting language like PHP.

The easiest way to prepare data for importing into a spreadsheet is going to be creating a CSV file. Here's an example CSV file which will open fine in Excel:

column_a,column_b
1,2
5,7
8.988,abcdef

CSV files are just plain text, with the fields separated by commas, so they are easy to create. If the data in your fields needs to contain commas, double quote marks, newlines, or a few other special cases, then things get more tricky.

If you really want to create an Excel-format spreadsheet, this is pretty difficult and you're going to want the help of a library. Here's how I would find a PHP Excel library: http://google.com/search?q=php+create+excel+spreadsheet

We Are All Monica
  • 13,000
  • 8
  • 46
  • 72
  • Hi jnylen, this is exactly what I want to do. I also have access to PHP for this project so that will be doable. Does this look like a good library for this task? http://phpexcel.codeplex.com/ – tdc May 24 '12 at 19:36
  • +1, the PHP Excel library is very good. If you need a real Excel file this is the way to go. – davidethell May 24 '12 at 19:36
  • I haven't used that library, but based on David's recommendation, I will try it if I ever need to do that. Generating a CSV file is going to be much easier though. – We Are All Monica May 24 '12 at 19:36
  • @jnylen I just need to be able to export an HTML table into a table form which can be opened in something like Office Excel; the library I linked may be overkill for that. It doesn't have to be an Excel file, just a spreadsheet file which can be opened by excel (if you get my meaning). Are there any lighter weight methods for doing this with PHP? Thank you! – tdc May 24 '12 at 19:49
  • Tom: Use a CSV file. I would just build it using string concatenation (this will look different depending on how your data is stored). – We Are All Monica May 24 '12 at 19:50
  • @jnylen: my data is stored in a single table, with 9 columns and a different user on each row. I have an admin page which allows my client to filter their view to see users only they specify (my making an SQL call and displaying results in html table). I need a button they can press to "download" a spreadsheet of the filtered results they've entered – tdc May 24 '12 at 20:10
  • It sounds like you already know how to run a MySQL query and display results to the user. This is the same task. Save the parameters of the filtered view in a form, and have the form POST to a page which sends the CSV file back to the user. On that page you'll just do the database query and return some text formatted like the code block in my answer. You'll need to set some headers to be sure the file will open in excel (see http://stackoverflow.com/a/398239/106302). Does that make sense? – We Are All Monica May 25 '12 at 13:54
1

You can't do this with JavaScript because it cannot create a downloadable file.

You must do this on the server. Any HTML page that is a table will automatically be converted BY EXCEL ITSELF if you send out the correct MIME header before streaming out the HTML file.

Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • If you lie to the client like that, and tell the browser that something is an Excel file when it's really a plain-text or HTML table, you'll probably get an Excel error message indicating that the file is corrupted. I wouldn't use this method for that reason. If you want to spit out plain text, use a CSV file. – We Are All Monica May 24 '12 at 19:35
0

You can't do it all in Javascript. You can write a jQuery function to convert a table to something like a CSV format, but you won't be able to tell the browser to let the user download it. You'll have to POST it to a server which can then serve back the file.

See this question for details.

Community
  • 1
  • 1
davidethell
  • 11,708
  • 6
  • 43
  • 63
0

I can be mistaken, but it may be possible using data: URI scheme

<a href="data:text/csv;charset=utf8;base64,...encoded_data...">
  download as Excel
</a>

It is not cross-browser, but can help.

scriptin
  • 3,060
  • 1
  • 24
  • 33