4

I had a html table on my webpage. I need an export functionality like, the user gets a choice of opening or saving it on his local machine upon button click. As i already had the data in table format ready, it needs to read it and export it on client side itself using browser capabilities with out any plugins (jquery,..). Pure JS would be really appreciated.

HTML Table on my webpage:

<table id="incidents">
<tr>
<td>data1</td>
<td>data1</td>
<td>data1</td>
</tr>
<tr>
<td>data2</td>
<td>data2</td>
<td>data2</td>
</tr>
<tr>
<td>data3</td>
<td>data3</td>
<td>data3</td>
</tr>
</table>

I need to export this table into a csv or excel sheet on the clients machine giving the options of open or save

  • don't think that's gonna happen, a browser cannot write or read files on the client – Ben Oct 15 '11 at 20:04
  • 1
    I think you can now write files with the HTML 5 File API – meleyal Oct 15 '11 at 20:09
  • Can you post a snippet of the table markup? – meleyal Oct 15 '11 at 20:16
  • I seen someone create a data url with the CSV data from a html table. I just can't find it anymore. But it is possible. Some urls http://somenotestoself.wordpress.com/2010/06/17/how-to-create-a-csv-file-in-javascript-and-get-the-save-as-dialog/ and http://stackoverflow.com/questions/4184944/javascript-download-data-to-file-from-content-within-the-page – Gerben Oct 15 '11 at 20:21
  • Possible duplicate of [Export to CSV using jQuery and html](https://stackoverflow.com/questions/16078544/export-to-csv-using-jquery-and-html) – Dave Jarvis Apr 21 '18 at 00:55

1 Answers1

9

Generate the contents of the CSV file as a string in JavaScript (I assume you're not asking SO to just write this code for you), then encode it as Base64 and generate a data: URI with the MIME type text/csv. Redirect the browser to that URI and it should trigger a download dialog for the user.

Community
  • 1
  • 1
Jordan Running
  • 102,619
  • 17
  • 182
  • 182