Angular2 - ERROR TypeError: Cannot read property '0' of undefined
When tried Angular2 Demo, I got below issue:
core.es5.js:1020 ERROR TypeError: Cannot read property '0' of undefined
at parse_xlml_xml (xlsx.js:14380)
at parse_xlml (xlsx.js:14763)
at Object.readSync [as read] (xlsx.js:18888)
at FileReader.reader.onload (sheetjs.component.ts:39)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.es5.js:3890)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runGuarded (zone.js:151)
at FileReader.
Can you share the file you tested with?
I use the demo source from Git repository : https://github.com/SheetJS/js-xlsx/tree/a7d37797240c38ee0d84132b26733cb4955b687b/demos/angular2
The error stack shows that you tried to read a file (FileReader.reader.onload) -- test if that file is a valid Excel file
I used the same excel sheet provided in demo : https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls
That error shows up on files that are suspected to be XML or HTML files. The linked file is an actual BIFF8 XLS file. Can you look at the contents of the file and see if you have the original data?
This is what I see when running ng serve in the demo directory and drag-dropping the receipts file:
I checked the downloaded excel file and it has content.
I was having this issue with Nodejs, trying to read the xlsx file from an axios http GET. Turns out if I changed the GET request responseType to 'arraybuffer' and then read it like so:
const workbook = xlsx.read(file.data, { type: 'array' });
it worked.
Hope this helps anyone else, might be applicable to set your angular httpClient request options.