0

I would like to export the content of a html table with javascript.

First I would walk through all TD-elements and collect the data to send it in one big POST-query to another PHP-Page, that generates CSV-Excel from it.

Or could I use another framework, that already includes this functionality?

rubo77
  • 15,234
  • 23
  • 111
  • 195

3 Answers3

1

Here is a useful plugin that converts a table to CSV string.

halilb
  • 3,785
  • 20
  • 28
  • 1
    That page seems not be obtained anymore, I copied the source to github: https://github.com/rubo77/table2CSV – rubo77 Jun 19 '13 at 18:48
1

window.open(MIMEtype, dataContainerItem) can be used to export the html table data directly from browser without making a server trip. But as per web standards it has some limitations and also we can't handle the output file name. Simple steps to do so are -

  1. wrap your html table into a container (DIV).
  2. pass the html content of this container(DIV) html to window.open along with MIME type.

ex.

window.open('data:application/vnd.ms-excel,' + encodeURIComponent( $('div[id$=divTableDataHolder]').html()));

You may refer- http://codepattern.net/Blog/post/jQuery-export-table-data-into-MS-Excel

and awesome details on its limitations at - Export to CSV using jQuery and html

Community
  • 1
  • 1
Anil Kumar
  • 143
  • 13
0

Use Data URIs and table2CSV:

var data = $table.table2CSV({delivery:'value'});

$('<a></a>')
    .attr('id','downloadFile')
    .attr('href','data:text/csv;charset=utf8,' + encodeURIComponent(data))
    .attr('download','filename.csv')
    .appendTo('body');

$('#downloadFile').ready(function() {
    $('#downloadFile').get(0).click();
});
Italo Borssatto
  • 12,895
  • 6
  • 58
  • 81
  • TableCSV has some bugs and is not developed any more, but there is another fork: https://github.com/ZachWick/TableCSVExport that already implemented some more new features (emptyValue, showHiddenRows, rowFilter) – rubo77 Nov 18 '13 at 14:28