简体   繁体   中英

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

It uses the following 2 js libraries:

https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.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.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. I am a java developer and this language kind of boggles my mind.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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