欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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>

 

 

 

 

 

相关标签: JS导出文件