Biswajeet gope
Biswajeet gope

Reputation: 331

How to extract a cell value from a given .xlsx sheet in a workbook using "xlsx" package from node?

I want to extract a cell value from a given .xlsx file from a particular row and column in a given sheet from a workbook using a npm module ".xlsx". Please suggest me any way to extract the value so that I can use the test data from the sheet.

Given I was able to go to a particular sheet

const xlsx =require("xlsx"); 
const wb=xlsx.readFile("test_data.xlsx"); 
const ws=wb.Sheets["Sheet1"] 

Upvotes: 1

Views: 7485

Answers (2)

Nagendra Pantham
Nagendra Pantham

Reputation: 59

You could load the workbook into memory, retrieve the sheet & then log out the cells in order to debug that you are looking at the correct information:

const workbook = xlsx.readFile('file path')
let worksheet = workbook.Sheets['Sheet name'];
let cell = worksheet['C3'].v; // Column C, Row 3 (assuming there's data there)

console.log('Cell data', cell)

If it all checks out you can use this information in loops, maps or any other means to render.

Upvotes: 3

Darshana Pathum
Darshana Pathum

Reputation: 669

Try to use this. You can install const xlsx-populate

Then this the function

 XlsxPopulate.fromFileAsync("./Book1.xlsx")
        .then(workbook => {

            // choose the sheet and a cell.
            const value = workbook.sheet("Sheet1").cell("A1").value();

            console.log(value);
        });

Upvotes: 1

Related Questions