javascript - 使用 JavaScript 将 HTML 表导出到 CSV 时保留前导零

标签 javascript html excel csv export-to-csv

以下演示允许您通过单击“导出到 CSV”按钮将 HTML 表导出为 CSV。

但是,如果您查看导出的 CSV,您会注意到最后一行:

“00001”被 chop 为“1”

function download_csv(csv, filename) {
    var csvFile;
    var downloadLink;

    // CSV FILE
    csvFile = new Blob([csv], {type: "text/csv"});

    // Download link
    downloadLink = document.createElement("a");

    // File name
    downloadLink.download = filename;

    // We have to create a link to the file
    downloadLink.href = window.URL.createObjectURL(csvFile);

    // Make sure that the link is not displayed
    downloadLink.style.display = "none";

    // Add the link to your DOM
    document.body.appendChild(downloadLink);

    // Lanzamos
    downloadLink.click();
}

function export_table_to_csv(html, filename) {
	var csv = [];
	var rows = document.querySelectorAll("table tr");
	
    for (var i = 0; i < rows.length; i++) {
		var row = [], cols = rows[i].querySelectorAll("td, th");
		
        for (var j = 0; j < cols.length; j++) 
            row.push(cols[j].innerText);
        
		csv.push(row.join(","));		
	}

    // Download CSV
    download_csv(csv.join("\n"), filename);
}

document.querySelector("#button2").addEventListener("click", function () {
    var html = document.querySelector("table").outerHTML;
	export_table_to_csv(html, "table.csv");
});
<table border="1px">
<thead>
	<tr>
	<th>ID</th>
	<th>PROVINCE</th>
	<th>DIVISION</th>	
	<th>NAME</th>
	</tr>
</thead>
<tbody>
	<tr><td>76363</td><td>Province1</td><td>AA</td><td>NAME1</td></tr>
	<tr><td>76371</td><td>Province2</td><td>AB</td><td>NAME2</td></tr>
	<tr><td>76388</td><td>Province3</td><td>AC</td><td>NAME3</td></tr>
	<tr><td>76424</td><td>Province4</td><td>AD</td><td>NAME4</td></tr>
	<tr><td>00001</td><td>undefined</td><td>undefined</td><td>undefined</td> 
</tr>
</tbody>
</table>
			
<button id="button2">Export to CSV</button>

我想保留可能放入表中的任何数据的前导零。

唯一的方法是在以 0 开头的数字之前添加撇号 '。

换句话说:00001将变成'00001

我假设解决方案是添加:

IF <td> starts with 0 add ' statement.

如何做到这一点?

最佳答案

function download_csv(csv, filename) {
    var csvFile;
    var downloadLink;

    // CSV FILE
    csvFile = new Blob([csv], {type: "text/csv"});

    // Download link
    downloadLink = document.createElement("a");

    // File name
    downloadLink.download = filename;

    // We have to create a link to the file
    downloadLink.href = window.URL.createObjectURL(csvFile);

    // Make sure that the link is not displayed
    downloadLink.style.display = "none";

    // Add the link to your DOM
    document.body.appendChild(downloadLink);

    // Lanzamos
    downloadLink.click();
}

function export_table_to_csv(html, filename) {
	var csv = [];
	var rows = document.querySelectorAll("table tr");
	
    for (var i = 0; i < rows.length; i++) {
		var row = [], cols = rows[i].querySelectorAll("td, th");
		
        for (var j = 0; j < cols.length; j++) 
            row.push(cols[j].innerText[0]=='0' ? ("'" + cols[j].innerText) : cols[j].innerText);
        
		csv.push(row.join(","));		
	}

    // Download CSV
    download_csv(csv.join("\n"), filename);
}

document.querySelector("#button2").addEventListener("click", function () {
    var html = document.querySelector("table").outerHTML;
	export_table_to_csv(html, "table.csv");
});
<table border="1px">
<thead>
	<tr>
	<th>ID</th>
	<th>PROVINCE</th>
	<th>DIVISION</th>	
	<th>NAME</th>
	</tr>
</thead>
<tbody>
	<tr><td>76363</td><td>Province1</td><td>AA</td><td>NAME1</td></tr>
	<tr><td>76371</td><td>Province2</td><td>AB</td><td>NAME2</td></tr>
	<tr><td>76388</td><td>Province3</td><td>AC</td><td>NAME3</td></tr>
	<tr><td>76424</td><td>Province4</td><td>AD</td><td>NAME4</td></tr>
	<tr><td>00001</td><td>undefined</td><td>undefined</td><td>undefined</td> 
</tr>
</tbody>
</table>
			
<button id="button2">Export to CSV</button>

关于javascript - 使用 JavaScript 将 HTML 表导出到 CSV 时保留前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953607/

相关文章:

php - 从 php 到 JavaScript 的数组

javascript - JQuery:动态调用函数

excel - 遍历 VBA (Excel) 中的列

node.js - 在 node.js 中,如何评估 excel 中包含对同一工作簿中其他工作表中单元格的引用的公式?

javascript - 在 HTML5 的元素(超链接)中包装表格行

javascript - 按名称调用 Javascript 函数

html - Bootstrap 4 Beta 显示无/隐藏问题

css - 位置 : absolute element 内的水平居中

javascript - 为什么通过 Javascript 创建脚本标签,而不是使用 defer 或 async 脚本标签属性?

php - 基于数组值的计算