我正在尝试读取 excel 文件并使用 XLSX 将其转换为 JSON 格式,但无法做到这一点。当文件位于本地计算机上时,任何人都可以建议转换方法吗?
通过输入选择您本地机器的 Excel 表。在那之后,
您的 Excel 数据将显示为 JSON 字符串。
function Upload() {
const fileUpload = (document.getElementById('fileUpload'));
const regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xls|.xlsx)$/;
if (regex.test(fileUpload.value.toLowerCase())) {
let fileName = fileUpload.files[0].name;
if (typeof (FileReader) !== 'undefined') {
const reader = new FileReader();
if (reader.readAsBinaryString) {
reader.onload = (e) => {
processExcel(reader.result);
};
reader.readAsBinaryString(fileUpload.files[0]);
}
} else {
console.log("This browser does not support HTML5.");
}
} else {
console.log("Please upload a valid Excel file.");
}
}
function processExcel(data) {
const workbook = XLSX.read(data, {type: 'binary'});
const firstSheet = workbook.SheetNames[0];
const excelRows = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[firstSheet]);
console.log(excelRows);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Process Excel File</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.0/xlsx.full.min.js"></script>
</head>
<body>
<input class="upload-excel" type="file" id="fileUpload" onchange="Upload()"/>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)