使用JavaScript将HTML表导出为CSV时保留前导零

时间:2018-12-28 04:13:40

标签: javascript html excel csv export-to-csv

下面的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.

该怎么做?

2 个答案:

答案 0 :(得分: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[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>

答案 1 :(得分:0)

使用Excel或任何其他表格类型程序打开文件时,该列将被解释为数字,并且前导零被删除。 但是,如果您以原始格式打开文件(例如在记事本中),或告诉Excel将列解释为文本,则会看到前导零。 使用导出的CSV的人可能会知道这一点。