将HTML表格导出到客户端的csv文件 [英] Export HTML table to csv file on client side

查看:95
本文介绍了将HTML表格导出到客户端的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有一张html表格。我需要一个导出功能,用户在点击按钮时可以选择打开或保存在本地机器上。
因为我已经准备好了表格数据,所以需要读取它并使用浏览器功能将其导出到客户端本身,无需任何插件(jquery,..)。纯粹的JS将非常感激。

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表格:

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>

我需要将此表导出到客户端计算机上的csv或excel表中,并提供open或保存

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

推荐答案

在JavaScript中以CSV文件的形式生成CSV文件的内容(我假设您并不是要求为您编写此代码),然后将其编码为Base64 并生成一个数据:URI ,其MIME类型为 text / csv 。将浏览器重定向到该URI,它应该为该用户触发一个下载对话框。

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.

这篇关于将HTML表格导出到客户端的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆