Java使用POI完成excel文件上传解析并保存到数据库

85 阅读1分钟

废话不多说,上代码~~~

/*
	导包都是poi位置
*/
// import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    XSSFWorkbook wb = new XSSFWorkbook(文件地址+文件名);
    
    XSSFSheet sheetAt = wb.getSheetAt(0);
    //最后一行行标,比行数小1
    int lastRowIndex = sheetAt.getLastRowNum();
    
//	System.out.println("lastRowIndex: " + lastRowIndex);
    
    for (int i = 1; i <= lastRowIndex; i++) {
//       System.out.println("rIndex: " + i);
        Row row = sheetAt.getRow(i);
    
        if (row != null) {
            
            //可以换别的封装,List等等......
            实体类 别名 = new 实体类();
    				
//        System.out.println(row.getCell(2));
    
//        System.out.println(row.getCell(3));
    
            //row.getCell(0)是第一个单元格数据,根据需求类型装换数据类型,这里是String
            别名.set实体字段(row.getCell(0).toString());
    
            别名.set字段名(row.getCell(Long.valueOf(字段名)));
            }
    
        	//测试解析之后封装到实体类中的数据
            //System.out.println(别名.toString());
    
            service.addEData(别名);
        }
    }