![](/img/trans.png)
[英]How to preserve leading zero when making it into a string in Javascript?
[英]Preserve a Leading Zero When Exporting HTML Table To CSV With JavaScript
下面的DEMO允许您通过单击“导出为CSV”按钮将HTML表格导出为CSV。
但是,如果您查看导出的CSV,则会在最后一行中注意到:
“ 00001”被截断为仅“ 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>
当使用Excel或任何其他表格类型程序打开文件时,该列将被解释为数字,并且前导零被删除。 但是,如果您以原始格式打开文件(例如在记事本中),或告诉Excel将列解释为文本,则会看到前导零。 使用您导出的CSV的用户可能会知道这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.