Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of a cell at a specified position in an excel sheet using JAVA

How to get the value of a specific cell from a .xlsm file using java ..?? I want to fetch the cell value by specifying the particular row and column for example i need the cell value at row 1 and column C1 or row5 and column C6 ... I am getting the values by specifying the row and column number like this

XSSFRow row = sheet.getRow(4); // 4 is the row number

cell = row.getCell(4); // 4 is the column number

But this is working only if the sheet has column starting from A,B,C,D...so on...when i try with the same coding to fetach another sheet but it does not work... In this sheet, column starts from C,D,E ... so on

Can any one help me out to get to know what can i use therr to get the specified result ?

like image 799
Kartik P Avatar asked Dec 27 '12 10:12

Kartik P


1 Answers

You probably want to use the CellReference utility class to help you out.

You can then do something like:

 Sheet sheet = workbook.getSheet("MyInterestingSheet");

 CellReference ref = new CellReference("B12");
 Row r = sheet.getRow(ref.getRow());
 if (r != null) {
    Cell c = r.getCell(ref.getCol());
 }

That will let you find the cell at a given Excel-style reference

like image 88
Gagravarr Avatar answered Oct 12 '22 23:10

Gagravarr