5

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);
    }
}

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, have any idea about It is converting large numbers like 3520000000000 into 3.52E+12 ?
@NomanAli Not sure, need to refer the docs for the same. I guess this is taking existing column setting from excel sheet. Probably create a new question for the same if this one has solved the problem you had originally described.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.