开发者

Save HTML Table to CSV Using Javascript [duplicate]

This question already has answe开发者_开发问答rs here: How to download html table as csv along with images src (2 answers) Closed last year.

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.


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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜