Export HTML table to Excel - using jQuery or Java

前端 未结 9 1687
慢半拍i
慢半拍i 2020-12-06 07:13

I\'ve a HTML table on my JSP page, that I want to be exported to Excel on a button click.

What would be the best way of going about this?

(For ex., how would

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 07:26

    Excel can load CSV (comma-separated value) files, which are basically just files with everything that would go into separate Excel cells separated by comma.

    I don't know enough about how jQuery can handle pushing information into a file that you would download, but it seems a jQuery library has been written that at least transforms html tables to CSV format, and it is here: http://www.kunalbabre.com/projects/table2CSV.php

    Edit (February 29, 2016): You can use the table2csv implementation above in conjunction with FileSaver.js (which is a wrapper for the HTML5 W3C saveAs() spec).

    The usage will end up looking something like:

    var resultFromTable2CSV = $('#table-id').table2CSV({delivery:'value'});
    
    var blob = new Blob([resultFromTable2CSV], {type: "text/csv;charset=utf-8"});
    
    saveAs(blob, 'desiredFileName.csv');
    

提交回复
热议问题