将Excel数据设置为Java Bean

时间:2018-08-11 07:04:41

标签: excel pojo

I want to set the excel data to a Java POJO

My Excel  will be 
firstName lastName amount
A            B       E
C            D       F

我创建了与excel标头完全匹配的Java Bean

public class ExcelBean {
    private String firstName;
    private String lastName;
    private String amount;
    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;
    }
    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }
}

以下代码执行excel行。遍历每一列并获取单元格数据

static File file;
    static FileInputStream fileInputStream;
    static XSSFWorkbook workBook;
    static XSSFSheet sheet;
    static XSSFRow row;
    static XSSFCell cell;
    static int rowNum;
    static int j;
    private static final String filePath = "Excel.xlsx";
    public static XSSFSheet getSheet() throws IOException {
        try {
            file = new File(filePath);
            fileInputStream = new FileInputStream(file);
            workBook = new XSSFWorkbook(fileInputStream);
            sheet = workBook.getSheetAt(0);
        } catch (Exception e) {
            System.out.println("Error while trying to get the sheet " + e.getMessage());
        }
        return sheet;
    }


    public static void getExcelData(XSSFSheet sheet) {
        int totalRows = sheet.getLastRowNum()+1;
        int totalColums = sheet.getRow(0).getLastCellNum();
        ExcelBean excelBean=new ExcelBean();
        for(int i=0;i<totalRows;i++){
            row=sheet.getRow(i);
            for(int j=0;j<totalColums;j++){ 
                String fieldValue = row.getCell(j).getStringCellValue();
                System.out.println(fieldValue); 
            }   
        }
    }


    public static void main(String[] args) throws IOException{
        sheet=ExcelUtil.getSheet();
        ExcelUtil.getExcelData(sheet);
    }

我的意图是从excel获取值并将其设置为我不知道的java bean。谁能帮助我实现此功能。

1 个答案:

答案 0 :(得分:1)

像这样更新getExcelData()函数

public static void getExcelData(XSSFSheet sheet) {
        int totalRows = sheet.getLastRowNum()+1;
        int totalColums = sheet.getRow(0).getLastCellNum();
        ExcelBean excelBean=new ExcelBean();
        for(int i=0;i<totalRows;i++){
            row=sheet.getRow(i);
            for(int j=0;j<totalColums;j++){ 
                String fieldValue = row.getCell(j).getStringCellValue();
                System.out.println(fieldValue); 
                switch(j)
                {
                  case 0:excelBean.setFirstName(fieldValue);break;
                  case 1:excelBean.setLastName(fieldValue);break;
                  case 2:excelBean.setAmount(fieldValue);break;
                  default:break;
                 }
            }   
        }
    }