// 批量区域数据导入
@Action(value = "area_batchImport")
public String batchImport() throws IOException {
List<Area> areas = new ArrayList<Area>();
// 编写解析代码逻辑
// 基于.xls 格式解析 HSSF
// 1、 加载Excel文件对象
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
// 2、 读取一个sheet
HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
// 3、 读取sheet中每一行
for (Row row : sheet) {
// 一行数据 对应 一个区域对象
if (row.getRowNum() == 0) {
// 第一行 跳过
continue;
}
// 跳过空行
if (row.getCell(0) == null
|| StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
continue;
}
Area area = new Area();
area.setId(row.getCell(0).getStringCellValue());
area.setProvince(row.getCell(1).getStringCellValue());
area.setCity(row.getCell(2).getStringCellValue());
area.setDistrict(row.getCell(3).getStringCellValue());
area.setPostcode(row.getCell(4).getStringCellValue());
// 基于pinyin4j生成城市编码和简码
String province = area.getProvince();
String city = area.getCity();
String district = area.getDistrict();
province = province.substring(0, province.length() - 1);
city = city.substring(0, city.length() - 1);
district = district.substring(0, district.length() - 1);
// 简码
String[] headArray = PinYin4jUtils.getHeadByString(province + city
+ district);
StringBuffer buffer = new StringBuffer();
for (String headStr : headArray) {
buffer.append(headStr);
}
String shortcode = buffer.toString();
area.setShortcode(shortcode);
// 城市编码
String citycode = PinYin4jUtils.hanziToPinyin(city, "");
area.setCitycode(citycode);
areas.add(area);
}
// 调用业务层
areaService.saveBatch(areas);
return NONE;
}