jquery - Reading Excel file using SheetJs - Javascript - Stack Overflow

admin2025-04-03  1

I am trying to read Excel file using SheetJs

But getting following error.

Uncaught TypeError: Cannot read property '0' of undefined

Here is my file handler function

function handleFileSelect(evt) {
    //Get the files from Upload control
    var files = evt.target.files;
    var i, f;
    //Loop through files
    for (i = 0, f = files[i]; i != files.length; ++i) {
        var reader = new FileReader();
        var name = f.name;
        reader.onload = function (evt) {
            var data = evt.target.result;

            var result;
            /* convert from workbook to array of arrays */
            var first_worksheet = data.Sheets[data.SheetNames[0]];
            var data = XLSX.utils.sheet_to_json(first_worksheet, {header:1});
            alert(result[0].Column1);
        };
        reader.readAsArrayBuffer(f);
    }
}

I am trying to read Excel file using SheetJs

But getting following error.

Uncaught TypeError: Cannot read property '0' of undefined

Here is my file handler function

function handleFileSelect(evt) {
    //Get the files from Upload control
    var files = evt.target.files;
    var i, f;
    //Loop through files
    for (i = 0, f = files[i]; i != files.length; ++i) {
        var reader = new FileReader();
        var name = f.name;
        reader.onload = function (evt) {
            var data = evt.target.result;

            var result;
            /* convert from workbook to array of arrays */
            var first_worksheet = data.Sheets[data.SheetNames[0]];
            var data = XLSX.utils.sheet_to_json(first_worksheet, {header:1});
            alert(result[0].Column1);
        };
        reader.readAsArrayBuffer(f);
    }
}
Share Improve this question asked Oct 24, 2017 at 12:02 Noman AliNoman Ali 3,34013 gold badges47 silver badges79 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You need to first read this data as an XLSX sheet first (Refer page 11)

workbook = XLSX.read(data, {type: 'binary'});

Followed by getting sheetnames from workbook (page 13)

var first_sheet_name = workbook.SheetNames[0];
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743625189a213761.html

最新回复(0)