1、使用 XMLHttpRequest
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>Read Excel with JavaScript</title></head><body><script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.full.min.js"></script><script>var xhr = new XMLHttpRequest();xhr.open("GET", "./测试.xlsx", true);xhr.responseType = "arraybuffer"; xhr.onreadystatechange = function () {if (xhr.readyState === 4 && xhr.status === 200) {var data = new Uint8Array(xhr.response); var workbook = XLSX.read(data, { type: "array" });var firstSheetName = workbook.SheetNames[0];var worksheet = workbook.Sheets[firstSheetName];var jsonData = XLSX.utils.sheet_to_json(worksheet);console.log(jsonData);}};xhr.send();</script></body>
</html>
2、使用 input
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>Read Excel with JavaScript</title></head><body><input type="file" id="fileInput" /><pre id="output"></pre><script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.full.min.js"></script><script>document.getElementById("fileInput").addEventListener("change", handleFile, false);function handleFile(event) {const file = event.target.files[0];const reader = new FileReader();reader.onload = function (e) {const data = new Uint8Array(e.target.result);const workbook = XLSX.read(data, { type: "array" });const firstSheetName = workbook.SheetNames[0];const worksheet = workbook.Sheets[firstSheetName];const jsonData = XLSX.utils.sheet_to_json(worksheet, {header: 1,});console.log(jsonData);};reader.readAsArrayBuffer(file);}</script></body>
</html>