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

5
  • don't think that's gonna happen, a browser cannot write or read files on the client Commented Oct 15, 2011 at 20:04
  • 1
    I think you can now write files with the HTML 5 File API Commented Oct 15, 2011 at 20:09
  • Can you post a snippet of the table markup? Commented Oct 15, 2011 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 somenotestoself.wordpress.com/2010/06/17/… and stackoverflow.com/questions/4184944/… Commented Oct 15, 2011 at 20:21
  • Possible duplicate of Export to CSV using jQuery and html Commented Apr 21, 2018 at 0:55

1 Answer 1

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.