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

前端 未结 9 1457
情书的邮戳
情书的邮戳 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

    I'm using POI and I'm uploading a simple program. Hope this will help you.

    Note: Remember to change filepath.

    Jars details: dom4j-1.6.1.jar, poi-3.9.jar,poi-ooxml-3.9.jar, poi-ooxml-schemas-3.11.jar, xmlbeans-2.6.0.jar

    My Data in Excel Table:

    ID   NAME  LASTNAME 
    1.0  Ena   Rana 
    2.0  Meena Hanly 
    3.0  Tina  Mounce 
    4.0  Dina  Cobain 
    

    Model or Pojo: NewEmployee.java

    public class NewEmployee {
         private Double id;
         private String firstName;
         private String lastName;
    
         public NewEmployee(){}
    
        public NewEmployee(Double id, String firstName, String lastName) {
            super();
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        public Double getId() {
            return id;
        }
    
        public void setId(Double id) {
            this.id = id;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }    
    }
    

    Main Method: ExcelToObject.java

    import java.io.File;
    import java.io.FileInputStream;
    import java.util.ArrayList;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class ExcelToObject {
    
        public static void main(String[] args) {
             try
              {
                  FileInputStream file = new FileInputStream(new File("/home/ohelig/eclipse/New Worksheet.xlsx"));
    
                  //Create Workbook instance holding reference to .xlsx file
                  XSSFWorkbook workbook = new XSSFWorkbook(file);
    
                  //Get first/desired sheet from the workbook
                  XSSFSheet sheet = workbook.getSheetAt(0);
    
                  ArrayList employeeList = new ArrayList<>();
        //I've Header and I'm ignoring header for that I've +1 in loop
                  for(int i=sheet.getFirstRowNum()+1;i<=sheet.getLastRowNum();i++){
                      NewEmployee e= new NewEmployee();
                      Row ro=sheet.getRow(i);
                      for(int j=ro.getFirstCellNum();j<=ro.getLastCellNum();j++){
                          Cell ce = ro.getCell(j);
                        if(j==0){  
                            //If you have Header in text It'll throw exception because it won't get NumericValue
                            e.setId(ce.getNumericCellValue());
                        }
                        if(j==1){
                            e.setFirstName(ce.getStringCellValue());
                        }
                        if(j==2){
                            e.setLastName(ce.getStringCellValue());
                        }    
                      }
                      employeeList.add(e);
                  }
                  for(NewEmployee emp: employeeList){
                      System.out.println("ID:"+emp.getId()+" firstName:"+emp.getFirstName());
                  }
                  file.close();
              } 
              catch (Exception e) 
              {
                  e.printStackTrace();
              }
          }
    }
    

提交回复
热议问题