spring mvc + poi
@ApiOperation("意见表-解析人员名单excel")
@PostMapping(value = "/opinionExcel")
@ResponseBody
public ArrayList<Map> opinionWord(@RequestParam(value = "file") MultipartFile file) throws IOException {
ArrayList<Map> result = new ArrayList<>();
HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
HSSFSheet sheet = wb.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
HashMap<String, String> map = new HashMap<>();
HSSFRow row = sheet.getRow(i);//行
try {
for (int j = 0; j < 2; j++) {
String value = row.getCell(j).getStringCellValue();
if (j == 0) {
map.put("name", value);
} else {
map.put("id", value);
result.add(map);
}
}
} catch (Exception e) {
//姓名或身份证为空时,直接下一行
e.printStackTrace();
}
}
return result;
}
excel数据
姓名 身份证号码
小强 510122197010141000
小王 510122197010141001
小坤 510122197010141002
510122197010141003
小夏
小胡 510122197010141005
小习 510122197010141006