您当前的位置: 首页 > 

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

POI导入

java持续实践 发布时间:2019-10-18 16:49:52 ,浏览量:2

String fileName = file.getOriginalFilename();
Workbook workbook = null;
ExcelUtil eu = new ExcelUtil();
//判断文件类型
if (fileName.endsWith("xls")) {
    try {
        log.info("这是一个后缀是xls版本的,调用HSSFWorkbook老版本");
        workbook = new HSSFWorkbook(file.getInputStream());// 2003版本
    } catch (IOException e) {
        e.printStackTrace();
    }
} else if (fileName.endsWith("xlsx")) {
    try {
        log.info("这是一个后缀是xlsx版本的,调用XSSFWorkbook老版本");
        workbook = new XSSFWorkbook(file.getInputStream());// 2007版本
    } catch (IOException e) {
        e.printStackTrace();
    }
//不是excel文件就提示错误
} else {
    try {
        return  JsonResult.parameterError("文件不是Excel文件");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//获取到当前sheet页数据
Sheet sheet = workbook.getSheet("工作表");
int rows = sheet.getLastRowNum();
if (rows == 0) {
    try {
        return JsonResult.parameterError("数据为空请重新填写数据");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//获取Excel文档中的第一个表单
Sheet sht0 = workbook.getSheetAt(0);
Row ro = sht0.getRow(1);
boolean flag = false;
Cell cell = ro.getCell(4);
String cellName = null;
if (cell != null) {
    cellName = cell.getStringCellValue();
}
if (cellName != null && cellName.trim().length() > 0) {
    if (!"*性别".equals(cellName)) {
        throw new Exception("文件不是Excel文件");;
    }
} else {
    cell = ro.getCell(2);
    if (cell != null) {
        cellName = cell.getStringCellValue();
    }
    if (cellName == null || (cellName.trim().length() > 0 && !"*性别".equals(cellName))) {
       throw new Exception("文件不是Excel文件");;
    }
}

//对Sheet中的每一行进行迭代
int count=0;
for (Row r : sht0) {
    //如果当前行的行号(从0开始)未达到2(第三行)则从新循环
    int rnum = r.getRowNum() + 1;
    if (r.getRowNum()             
关注
打赏
1658054974
查看更多评论
0.0425s