How can I read a value related to Specific Column Names with Apache POI in Excel (xlsx).
Column Names is my 1st Row of Excel Sheet . value is the row Which have Y as cell value .
My Input is only Column Name , I need value for the Column name and Row which has value "Y"
here I if enter Column names as "Names" , it should return value "Jose" And "Age" it should return 23
I have tried and found two codes with different loop system , but getting some error
public static void main(String[] args) throws Exception {
File file = new File("D:\\xcel.xlsx");
FileInputStream inputStream = new FileInputStream(file);
Workbook wb = new XSSFWorkbook(inputStream);
Sheet sh = wb.getSheet("Sheet1");
// Find the Column number Which has column name and row number 0
Row row1 = sh.getRow(0);
int colNum = -1;
for (int i = 0 ;i<=row1.getLastCellNum();i++){
Cell cell1 = row1.getCell(i,Row.CREATE_NULL_AS_BLANK);
String cellValue1 = cell1.getStringCellValue();
if ("Employee".equals(cellValue1)){
colNum = i ;
break;}
}
// Find the Row number Which is "Y" and column number 1
int rowNum = -1;
for (int j = 0 ;j<=sh.getLastRowNum()+1;j++){
Row row2 = sh.getRow(j);
Cell cell2 = row2.getCell(1,Row.CREATE_NULL_AS_BLANK);
String cellValue2 = cell2.getStringCellValue();
if ("Y".equals(cellValue2))
{ rowNum = j ;
break;}
}
Row r = sh.getRow(rowNum);
String val = r.getCell(colNum).getStringCellValue();
System.out.println("Row number is "+rowNum);
System.out.println("Last row number :"+sh.getLastRowNum());
System.out.println("Column number is "+colNum);
System.out.println("Last Column number :"+sh.getRow(0).getLastCellNum());
System.out.println("Value is "+val);
}
When I run the above code , I get the column number even if there is a blank cell is present before it ,
but for row , if there is a blank cell present before it , i get error Exception in thread "main" java.lang.NullPointerException But it works fine if there is no black cell.
Again in this below code , I don't get any error
But if my column name is present in last cell ,then last cell number and my column number should be same , but I getting my column number is one less than the last cell number .
But for row number , its fine , I am getting same number .
This problem is applies to the first code also
public static void main(String[] args) throws Exception {
File file = new File("D:\\xcel.xlsx");
FileInputStream inputStream = new FileInputStream(file);
Workbook wb = new XSSFWorkbook(inputStream);
Sheet sh = wb.getSheet("Sheet1");
// Find the Column number Which has column name and row number 0
Row row1 = sh.getRow(0);
int colNum = -1;
for (Cell cell : row1) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim().equals("Employee")) {
colNum = cell.getColumnIndex(); }
}
}
// Find the Row number Which is "Y" and column number 1
int rowNum = -1;
for (Row row : sh) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim().equals("Y")) {
rowNum = row.getRowNum(); }
}
}
}
// Get the required value
Row r = sh.getRow(rowNum);
String val = r.getCell(colNum).getStringCellValue();
System.out.println("Row number is "+rowNum);
System.out.println("Last row number :"+sh.getLastRowNum());
System.out.println("Column number is "+colNum);
System.out.println("Last Column number :"+sh.getRow(0).getLastCellNum());
System.out.println("Value is "+val);