如何使用JAVA获取Excel工作表中指定位置的单元格值

时间:2021-08-02 20:24:25

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

如何使用java从.xlsm文件中获取特定单元格的值.. ??我想通过指定特定的行和列来获取单元格值,例如我需要第1行和第C1列或第5行和第C6列的单元格值...我通过指定行和列号来获取值,如下所示

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

XSSFRow row = sheet.getRow(4); // 4是行号

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

cell = row.getCell(4); // 4是列号

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

但是只有当工作表有从A,B,C,D开始的列时...这样才有效...当我尝试使用相同的编码来获取另一张工作表但它不起作用时...在此工作表中,列从C,D,E开始......等等

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

任何人都可以帮助我了解我可以使用什么来获得指定的结果?

2 个解决方案

#1


10  

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

您可能希望使用CellReference实用程序类来帮助您。

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

这将让您在给定的Excel样式引用中找到单元格

#2


0  

if that sheet has name,u can get the sheet name and use iterator.

如果该工作表有名称,您可以获取工作表名称并使用迭代器。

  Iterator iterator = workSheet.rowIterator();
  while(iterator.next){
       Row row = (Row) iterator.next();
        //u can iterate and use row.getCell(i)
  }

#1


10  

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

您可能希望使用CellReference实用程序类来帮助您。

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

这将让您在给定的Excel样式引用中找到单元格

#2


0  

if that sheet has name,u can get the sheet name and use iterator.

如果该工作表有名称,您可以获取工作表名称并使用迭代器。

  Iterator iterator = workSheet.rowIterator();
  while(iterator.next){
       Row row = (Row) iterator.next();
        //u can iterate and use row.getCell(i)
  }