第一步:在pom文件中导入如下依赖
cn.afterturn
easypoi-base
3.2.0
cn.afterturn
easypoi-web
3.2.0
cn.afterturn
easypoi-annotation
3.2.0
第二步在实体需要导入的字段上面添加如下注解
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
-------导包
@Excel(name = "配置项值") ----excel表头名
@TableField("PRIMARILY") ----数据库中的字段名
第三步代码
public JSONObject uploadExcel(MultipartFile multipartFile, HttpServletResponse response) {
try {
ImportParams params = new ImportParams();
params.setHeadRows(1);
List result = ExcelImportUtil.importExcel(multipartFile.getInputStream(), grantConfig.class, params);
//创建一个集合用于存放map数据
List> hashMaps = new ArrayList<>();
HashMap map;
for (grantConfig grantConfig : result) {
map = new HashMap<>();
map.put("primarily", grantConfig.getPrimarily());
map.put("critical", grantConfig.getCritical());
map.put("key", grantConfig.getKey());
map.put("remarks", grantConfig.getRemarks());
hashMaps.add(map);
}
insert(hashMaps);
JSONObject jsonObject = new JSONObject();
jsonObject.put("state", "success");
return
按钮
实现
//上传文件
upload.render({
elem: '#importFile',
url: '/grantConfig/uploadGrantFile',
accept: 'file',
done: function (res) {
if (res.state == 'success') {
layer.msg("导入成功", {icon: 1})
} else {
layer.msg("导入失败", {icon: 2})
}
}
});
------此段代码要放在layui.use