我尝试使用这些js库和代码段创建一个excel解析器:How to parse Excel file in Javascript/HTML5
它使用以下2个js库:
https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js
https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js
但是当我尝试上传任何xlsx文件时,我收到无效的标头错误: 我使用的是Mac和谷歌浏览器,并确认这些文件是真正的xlsx文件(创建新文件只是为了测试)。
xlsx.js:1660 Uncaught Header Signature: Expected d0cf11e0a1b11ae1 saw 504b030414000600CheckField
@ xlsx.js:1660check_get_mver
@ xlsx.js:986parse
@ xlsx.js:898readSync
@ xlsx.js:1212reader.onload
@ uploadfile.js:140
有没有办法解决这个问题,或者我做错了什么?我刚刚开始使用javascript /做前端的东西。我是一名java开发人员,这种语言让我大吃一惊。
我使用的代码是:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Parse Excel</title>
</head>
<body>
Hello World!
<div id="content">
<input type="file" id="input" />
</div>
</body>
</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
<script>
var filebutton = document.getElementById("input");
filebutton.addEventListener("change", filePicked, false);
function filePicked(oEvent) {
// Get The File From The Input
var oFile = oEvent.target.files[0];
var sFilename = oFile.name;
// Create A File Reader HTML5
var reader = new FileReader();
// Ready The Event For When A File Gets Selected
reader.onload = function(e) {
var data = e.target.result;
var cfb = XLSX.CFB.read(data, {type: 'binary'});
var wb = XLSX.parse_xlscfb(cfb);
// Loop Over Each Sheet
wb.SheetNames.forEach(function(sheetName) {
// Obtain The Current Row As CSV
var sCSV = XLSX.utils.make_csv(wb.Sheets[sheetName]);
var oJS = XLSX.utils.sheet_to_row_object_array(wb.Sheets[sheetName]);
$("#my_file_output").html(sCSV);
console.log(oJS)
});
};
// Tell JS To Start Reading The File.. You could delay this if desired
reader.readAsBinaryString(oFile);
}
答案 0 :(得分:0)
好吧,我通过巨大的反复试验和大量的浪费时间来了解它。我列出的导入顺序在jszip.js之前称为xlsx.js,但jszip.js需要在上面。