SheetJS / sheetjs

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
https://sheetjs.com/
Apache License 2.0
35.21k stars 7.99k forks source link

.xslx file parsing #824

Closed harminder-emobx-com closed 7 years ago

harminder-emobx-com commented 7 years ago

Hi

I am trying to parse excel file using this js library. I was successfully able to parse xls file. But when i am trying to parse .xlsx file its giving me error. Is their anything else require to be done. Please share.

I am using following code.

 {
            file = input.files[0];
            fr = new FileReader();
            fr.onload=function()
            {
              console.log("loaded");

              var content=fr.result;
              if(!rABS) content = new Uint8Array(content);
              var Workbook = XLSX.read(content, { type: 'binary' });
              console.log(Workbook);

        Workbook.SheetNames.forEach(function(sheetName) {
        var sheet= Workbook.Sheets[sheetName];  
         var roa = XLSX.utils.sheet_to_json(sheet);
        console.log(roa);
        roa.forEach(function(obj){
               console.log(obj); 
            $("#"+obj.Name).val(obj.Value);
        });

        });

            };
            var data= fr.readAsText(file);

        }

I am getting following error

Uncaught Error: Cannot find file [Content_Types].xml in zip
    at q (xlsx.full.min.js:12)
    at re (xlsx.full.min.js:12)
    at rv (xlsx.full.min.js:21)
    at sv (xlsx.full.min.js:21)
    at Object.cv [as read] (xlsx.full.min.js:21)
    at FileReader.fr.onload (registration_crud?_mode=edit&id=189_Student_process1:434)
SheetJSDev commented 7 years ago

Can you test against one of the known good files (try https://github.com/SheetJS/test_files/blob/master/AutoFilter.xlsx?raw=true )

harminder-emobx-com commented 7 years ago

Hi @SheetJSDev

I tested this file i got following response

xlsx.full.min.js:2 Uncaught Error: End of data reached (data length = 55274, asked index = 65021). Corrupted zip ?
    at n.checkIndex (xlsx.full.min.js:2)
    at n.setIndex (xlsx.full.min.js:2)
    at h.readCentralDir (xlsx.full.min.js:2)
    at h.load (xlsx.full.min.js:2)
    at new h (xlsx.full.min.js:2)
    at n.r.exports [as load] (xlsx.full.min.js:2)
    at new n (xlsx.full.min.js:2)
    at sv (xlsx.full.min.js:21)
    at Object.cv [as read] (xlsx.full.min.js:21)
    at FileReader.fr.onload (registration_crud?_mode=edit&id=189_Student_process1:420)

am i doing anything wrong

harminder-emobx-com commented 7 years ago

Hi @SheetJSDev

Please suggest

SheetJSDev commented 7 years ago

@harminder-emobx-com the problem is in the encoding. readAsText does not work with binary files. You should either be using readAsArrayBuffer or readAsBinaryString. Follow the examples from https://github.com/sheetjs/js-xlsx#parsing-workbooks .

sachinai commented 6 years ago

@harminder-emobx-com i think problem is in XLSXReaderService.readFile , you should pass the proper file for XLSX.

lathashree01 commented 4 years ago

readAsBinaryString solved the problem :)

mrobert201 commented 3 years ago

readAsBinaryString solved my problem too. Thank you!