public ResponseEntity<Void> testImportByPersonName() {
//名字重复
HashMap<String, String> tempMap = new HashMap<>();
//编号,名称
HashMap<String, String> personMap = new HashMap<>();
Workbook wb = null;
Sheet sheet = null;
Row row = null;
String cellData = null;
//Excel文件路径
String filePath = "S:\\桌面\\OA人员工号整理.xls";
//读取Excel文件
try {
wb = new HSSFWorkbook(new FileInputStream(filePath));
//如果文件不为空
if (wb != null) {
//获取第一个sheet
sheet = wb.getSheetAt(0);
//获取最大行数
int rownum = sheet.getPhysicalNumberOfRows();
//获取第一行
row = sheet.getRow(0);
//获取最大列数
int colnum = row.getPhysicalNumberOfCells();
//循环行
for (int i = 1; i < rownum; i++) {
row = sheet.getRow(i);
if (row != null) {
personMap.put(row.getCell(1).getStringCellValue(), row.getCell(0).getStringCellValue());
} else {
break;
}
}
System.out.println("数量:=" + personMap.size());
}
//遍历map
Set<String> codes = personMap.keySet(); //所有人员编号
for (String code : codes) {
//根据编号获取名称
String name = personMap.get(code);
//查询person
List<Person> personList = personRepository.findAllByName(name);
if (personList.size() > 1) {
tempMap.put(code, name);
break;
}
//更新编号
Person newPerson = personList.get(0);
newPerson.setCode(code);
Person savePerson = personRepository.save(newPerson);
log.info("=======人员名称=" + savePerson.getName() + "=======人员编号=" + savePerson.getCode() + "=====================================");
}
//打印名称重复的
log.info("=======================总数:" + tempMap.size() + "=================");
tempMap.keySet().stream().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.ok(null);
【liuyl】Java 读取excel数据
最新推荐文章于 2020-08-10 14:00:17 发布