JS导出CSV文件
程序员文章站
2022-05-26 19:15:22
...
JS通过table的id将数据导出为csv文件格式
<a class="btn" id="productqueryOutXls" target="">导出</a>
<table class="" id="alternatecolor">
<thead>
<tr>
<th>订单号</th>
<th>订单时间</th>
<th>订单状态</th>
<th>订单金额</th>
<th>支付方式</th>
</tr>
</thead>
<tbody>
<tr>
<td>1234567</td>
<td>2019-01-23 01:30:09</td>
<td>complete</td>
<td>$25.05</td>
<td>paypal</td>
</tr>
</tbody>
</table>
<script>
$(document).on("click", "#productqueryOutXls", function () {
var $trs = $("#alternatecolor").find("tr");
var str = "";
for (var i = 0; i < $trs.length; i++) {
var $tds = $trs.eq(i).find("td,th");
for (var j = 0; j < $tds.length; j++) {
str += $tds.eq(j).text() + ",";
}
str += "\n";
}
var aaaa = "data:text/csv;charset=utf-8,\ufeff" + str;
var link = document.createElement("a");
link.setAttribute("href", aaaa);
var date=new Date().getTime();
var filename = new Date(date).toLocaleDateString();
link.setAttribute("download", filename + ".csv");
link.click();
});
</script>
上一篇: csv导出工具
下一篇: 【Python】绘制PR曲线