SheetJSNode.js 的電子表格數(shù)據(jù)工具包
SheetJS 是 Node.js 的電子表格數(shù)據(jù)工具庫(kù),可用于處理 Excel 電子表格,以及其他相關(guān)功能。比如,導(dǎo)出表格、轉(zhuǎn)換 HTML 表格和 JSON 數(shù)組為 xlsx 文件。
示例
<!DOCTYPE html>
<header>
<meta charset="utf-8">
<title>read excel to json</title>
</header>
<body>
<div style="margin:20px">
<input type="file" onchange="change(this)" style="width: 320px;"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
<pre><p style="font-family: sans-serif;"></p></pre>
</div>
<script src="xlsx.full.min.js"></script>
<script>
var excelData;
function change(obj) {
if (!obj.files) {
return;
}
var file = obj.files[0];
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
excelData = XLSX.read(data, {
type: 'binary'
});
//excelData.SheetNames[0]是獲取Sheets中第一個(gè)Sheet的名字
//excelData.Sheets[Sheet名]獲取第一個(gè)Sheet的數(shù)據(jù)
var json = XLSX.utils.sheet_to_json(excelData.Sheets[excelData.SheetNames[0]]);
document.querySelector("p").innerHTML = JSON.stringify(json, null, "\t");
};
reader.readAsBinaryString(file);
}
</script>
</body>
</html>
頁(yè)面效果:
要讀取的文件:
結(jié)果:
評(píng)論
圖片
表情
