给你以下 excel 文件,数据格式以市-区-街道的关系保存到数据库中,数据库表大致字段为 id、pid、name、code
代码如下:
public boolean importData(MultipartFile file) {
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(file.getInputStream());
// 获取第一个工作表
Sheet sheet = workbook.getSheetAt(0);
// 创建区和街道的关系映射
Map<String, String> relationshipMap = new HashMap<>();
// 创建市和区的关系映射
Map<String, String> cityRelationshipMap = new HashMap<>();
String currentCity = null;
String currentDistrict = null;
// 遍历每一行
for (int rowIndex = 1; rowIndex < sheet.getLastRowNum() + 1; rowIndex++) {
Row row = sheet.getRow(rowIndex);
// 获取市列、区列和街道列的值
Cell cityCell = row.getCell(0);
Cell districtCell = row.getCell(1);
Cell streetCell = row.getCell(2);
Cell codeCell = row.getCell(3);
// 获取市、区和街道的值
String city = cityCell.getStringCellValue();
String district = districtCell.getStringCellValue();
String street = streetCell.getStringCellValue();
if (currentCity == null) {
currentCity = city;
}
if (currentDistrict == null) {
currentDistrict = district;
}
if (StringUtils.isNotBlank(city)) {
// 将市和区添加到关系映射中
currentCity = city;
cityRelationshipMap.put(currentCity, district);
} else if (StringUtils.isNotBlank(district)) {
// 将市和区添加到关系映射中
String previousDistrict = cityRelationshipMap.get(currentCity);
cityRelationshipMap.put(currentCity, previousDistrict + "," + district);
}
if (StringUtils.isNotBlank(district)) {
// 将区和街道添加到关系映射中
currentDistrict = district;
relationshipMap.put(currentDistrict, street + " " + codeCell.getStringCellValue());
} else if (StringUtils.isBlank(district) && StringUtils.isNotBlank(street)){
// 将街道追加到关系映射中
String previousStreet = relationshipMap.get(currentDistrict);
relationshipMap.put(currentDistrict, previousStreet + "," + street + " " + codeCell.getStringCellValue());
}
}
for (Map.Entry<String, String> entry : cityRelationshipMap.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
Region region = new Region();
String[] parts = spiltRegion(entry.getKey());
region .setRegionName(parts[0]);
region .setRegionCode(parts[1]);
this.save(region);
List<Region> districtList = new ArrayList<>();
String[] districts = entry.getValue().split(",");
for(String district : districts) {
String[] part = spiltRegion(district);
Region region1 = new Region();
region1.setRegionName(part[0]);
region1.setRegionCode(part[1]);
region.setParentId(region.getRegionId());
districtList.add(region1);
}
this.saveBatch(districtList);
for (Region region : districtList) {
ArrayList<Region> streetList = new ArrayList<>();
String key = region.getRegionName() + " " + region.getRegionCode();
String streets = relationshipMap.get(key);
String[] split = streets.split(",");
for (String s : split) {
String[] part = spiltRegion(s);
log.debug(s);
Region street = new Region();
street.setRegionName(part[0]);
street.setRegionCode(part[1]);
street.setParentId(region.getRegionId());
streetList.add(street);
}
this.saveBatch(streetList);
}
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public String[] spiltRegion(String input) {
Pattern pattern = Pattern.compile("^(.*?)\\n(\\d+)");
Matcher matcher = pattern.matcher(input);
String[] result = new String[2];
if (matcher.matches()) {
result[0] = matcher.group(1);
result[1] = matcher.group(2);
}
return result;
}