首先拿一份全国省市区的数据,存到Excel 表中 拿到数据的方式很多这里就不多说
表中的数据格式(这只是一部分的数据)
然后需要通过Excel表拿到表中的数据 (方式也有很多)
我这里使用的是 hutool-all工具包 实现 (这是工具包的文档地址:https://www.hutool.club/docs/#/)
里面有很多的工具使用很方便
下面开始插入代码实现
导入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
首先controller层接口
/**
*更新省市区
*/
@ApiOperation(value = "修改")
@PostMapping("/update/city")
public Result updateCity(@RequestParam("cityFiles") MultipartFile cityFiles) throws IOException {
//异步跟新到数据库
dictionaryService.uploadCityExcl(cityFiles.getInputStream());
return Result.ok().setData("上传成功");
}
当然接收Excel
需要与Excel表中相同的字段接收
@Data
public class DictionaryExcl implements Serializable {
//"行政区划代码"
private String nameId;
//"名称"
private String name;
}
字典表实体
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_sys_dictionary")
//@ApiModel(description = "字典表")
public class Dictionary extends Model<Dictionary> {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId(value = "id", type = IdType.AUTO)
//@ApiModelProperty(name = "id",value = "主键")