简体   繁体   English

解析带有JavaScript库的XLSX的无效标头签名

[英]Invalid header signature parsing XLSX with JavaScript library

I'm trying to create an excel parser using these to js libraries and code snippet found here : How to parse Excel file in Javascript/HTML5 我正在尝试使用这里的js库和代码片段创建一个Excel解析器: 如何在Javascript / HTML5中解析Excel文件

It uses the following 2 js libraries: 它使用以下2个js库:

https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.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 https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js

However when I try to upload any xlsx files I get an invalid header error: I'm using a Mac and Google Chrome and have confirmed the files are real xlsx files (created new one just to test). 但是,当我尝试上传任何xlsx文件时,出现了无效的标头错误:我使用的是Mac和Google Chrome浏览器,并确认这些文件是真实的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

Is there any way to work around this or perhaps I am doing something wrong? 有什么办法可以解决此问题,或者我做错了什么? I am just starting using javascript/doing front end stuff. 我只是开始使用javascript /做前端的东西。 I am a java developer and this language kind of boggles my mind. 我是Java开发人员,这种语言让我感到困惑。

The code I am using is: 我使用的代码是:

<!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);
}

Well, I got to the bottom of it through tremendous trial and error and a lot of wasted time. 好吧,我通过大量的尝试和错误以及大量的浪费时间来解决问题。 The order of the imports I listed called xlsx.js before jszip.js but jszip.js needed to be above. 我列出的导入顺序在jszip.js之前称为xlsx.js,但jszip.js必须在上面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM