How to convert my xlsx sheet to java object using Apache POI

前端 未结 9 1460
情书的邮戳
情书的邮戳 2020-12-14 22:06

can any one suggest me to convert my xlsx sheet to java object using Apache POI.

eq, my excel sheet contains two columns

  • emp_no emp_name
  • 01
9条回答
  •  一个人的身影
    2020-12-14 22:13

    For the given Scenario, I am assuming that each row of the sheet is representing an employee of which say first Column is keeping employee Number and second column is keeping Employee Name. so you can use the following:

    Employee{
      String empNo;
      String empName; 
    }
    

    Create a method of assigning the Employee information as

    assignEmployee(Row row){
        empNo = row.getCell(0).toString();
        empName = row.getCell(1).toString();
    }
    

    or if you want you can create a constructor for the same.

    Now you just need to iterate over each row to get/use the information using the above method.

    Employee emp = new Employee();
    Iterator itr = sheet.iterator();
        while(itr.hasNext()){
           Row row = itr.next();
           emp.assignEmployee(row);
          //  enter code here for the rest operation
    }
    

提交回复
热议问题