最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to use XLSX library in Javascript to parse an Excel file from a particular row - Stack Overflow

matteradmin9PV0评论

I am currently using the library XLSX in Javascript to convert an Excel file into JSON object and it is giving me the results as expected.

However, the code I am currently using parses the entire Excel sheet and I don't want that. I want to start parsing the file from a particular row (row number 14).

Can this be done using the XLSX? If yes, how?

Below is my current JS code (taken from )

var selectedFile;
document.getElementById("fileUpload")
    .addEventListener("change", function (event) {
        selectedFile = event.target.files[0]
    });
document.getElementById("upload")
    .addEventListener("click", function () {
        if (selectedFile) {
            var fileReader = new FileReader();
            fileReader.onload = function (event) {
                var data = event.target.result;

                var workbook = XLSX.read(data, {
                    type: "binary"
                });
                workbook.SheetNames.forEach(sheet => {
                    let rowObject = XLSX.utils.sheet_to_row_object_array(
                        workbook.Sheets[sheet]
                    );
                    let jsonObject = JSON.stringify(rowObject, null, '\t');
                    document.getElementById("jsonData").innerHTML = jsonObject;
                });
            };
            fileReader.readAsBinaryString(selectedFile);
        }
    });

I am currently using the library XLSX in Javascript to convert an Excel file into JSON object and it is giving me the results as expected.

However, the code I am currently using parses the entire Excel sheet and I don't want that. I want to start parsing the file from a particular row (row number 14).

Can this be done using the XLSX? If yes, how?

Below is my current JS code (taken from https://levelup.gitconnected./how-to-convert-excel-file-into-json-object-by-using-javascript-9e95532d47c5)

var selectedFile;
document.getElementById("fileUpload")
    .addEventListener("change", function (event) {
        selectedFile = event.target.files[0]
    });
document.getElementById("upload")
    .addEventListener("click", function () {
        if (selectedFile) {
            var fileReader = new FileReader();
            fileReader.onload = function (event) {
                var data = event.target.result;

                var workbook = XLSX.read(data, {
                    type: "binary"
                });
                workbook.SheetNames.forEach(sheet => {
                    let rowObject = XLSX.utils.sheet_to_row_object_array(
                        workbook.Sheets[sheet]
                    );
                    let jsonObject = JSON.stringify(rowObject, null, '\t');
                    document.getElementById("jsonData").innerHTML = jsonObject;
                });
            };
            fileReader.readAsBinaryString(selectedFile);
        }
    });
Share Improve this question asked May 27, 2020 at 8:43 Akhil KintaliAkhil Kintali 5462 gold badges11 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

I am answering my own question so that it might be helpful for anyone later.

let rowObject = XLSX.utils.sheet_to_row_object_array
(
     workbook.Sheets[sheet], {range: 14}
);

The parameter range can be added in the given function which is a part of the code.

The Excel parses from line 14 now and not the beginning of the page.

Post a comment

comment list (0)

  1. No comments so far