1

I have an html table rendered page and I want to save it as CSV file on the client's machine if they click the save button.

This should be using javascript in Firefox and work with all IE versions.

BlackJack
  • 3,993
  • 1
  • 16
  • 22
Anbu
  • 863
  • 3
  • 10
  • 16
  • 3
    javascript doesn't have write permissions on the client's machine... – David Hedlund Mar 25 '10 at 11:55
  • Going back to the server to get the data in the other format is the simplest way (and also the only one that is in at all reliable). As far as the client is concerned, all you need is a simple link. – Quentin Mar 25 '10 at 12:02
  • You could have the user get a pop-up for the download after clicking save: See http://stackoverflow.com/questions/16078544/export-to-csv-using-jquery-and-html for details. – Cody G May 20 '16 at 18:27

4 Answers4

2

You cannot do it using javascript capabilities, since javascript has no permission to write on client machine, instead you can send request to server to create csv file and send it back to client.

Artem Barger
  • 38,615
  • 9
  • 54
  • 80
1

To convert your HTML table to CSV see How can I convert an HTML table to CSV?

Community
  • 1
  • 1
Santiago Cepas
  • 3,912
  • 2
  • 23
  • 31
0

You can't force them to download a file using JavaScript.

Best bet is to use PHP or ASP to create the file for them server side and have a link to them to download the file.

you will need to have two nested for loops, the outer one iterates for each row tag, the inner one for each column/cell tag with in the row. I'm not CSV can handle merged cells though, but with it being a HTML table, I would imagine that's not such an issue.

openfile()
for(i = 0 ; i < num_rows ; i = i + 1)
{
    for(j = 0 ; j < num_culumns ; j = j + 1)
    {
        write_to_file( row[i]. column[j].data)
        write_to_file( cell_denominator )
    }
    write_to_file( end_of_line_denominator )
    write_to_file( new_line )
}
close_file()
thecoshman
  • 7,772
  • 5
  • 52
  • 77
0

The simplest way is to do this server-side. If you can't go through a server, you can still open the data file in a Data URI, but this is not compatible with all versions of IE. Saving a file to disk directly is extremely non-portable, and impossible in many cases.

Victor Nicollet
  • 23,569
  • 3
  • 52
  • 88