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.

4
  • 3
    javascript doesn't have write permissions on the client's machine... Commented Mar 25, 2010 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. Commented Mar 25, 2010 at 12:02
  • You could have the user get a pop-up for the download after clicking save: See stackoverflow.com/questions/16078544/… for details. Commented May 20, 2016 at 18:27
  • 3
    Does this answer your question? How to download html table as csv along with images src Commented Jul 28, 2021 at 20:39

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

would be nice if you pick one and accept it, if you want people keep answering on your further questions :D
1

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

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.