2 years ago

#50958

test-img

Aakash Giri

Error: Corrupted zip : can't find end of central directory "When trying to read a slice of .xlsx file."- XLSX

I am trying to read the .xlsx file using FileReader and XLSX module. So If I am trying to read the whole .xlsx file then it's working fine. But I just want to read only the top 5 to 10 lines of .xlsx file. so that's why instead of passing an original file which can be quite large some time greater than 100mb. I am just passing a slice of the file to the FileReader so it can read the file faster otherwise if I pass the original file which can be 100mb then it will take too much time in processing and I do not want to process a whole file to just read top 10 lines.
Here is the code :

onFile(event:any){
let data = event.target.files[0];
let fileReader = new FileReader();
fileReader.onload = (e) => {
  let arrayBuffer = e.target.result;
  console.log(arrayBuffer);
  let workbook = XLSX.read(arrayBuffer, {type:"array"});
  let first_sheet_name = workbook.SheetNames[0];
  let worksheet = workbook.Sheets[first_sheet_name];
  console.log(XLSX.utils.sheet_to_json(worksheet,{raw:true}));
}
fileReader.readAsArrayBuffer(data.slice(0,500));
}

so I think the problem here is during slicing I am trying to slice starting 500 bytes using the slice method and then passing it to the FileReader. so my question is here how can I slice a .xlsx file properly because using the current way file getting corrupt.

Note: other files like .csv or .tsv working fine even if I am slicing them using the current way. I was also able to read the .xlsx file using the same way a few months ago. but I don't know why I am facing this issue now.

javascript

xlsx

sheetjs

0 Answers

Your Answer

Accepted video resources