后端controller
根据自己的实体类下载Excel文件,在实体类中添加注释
@Excel(name="")
App 是自己的实体类类名
/**
* 导入住宿地点
*
*/
@Log(title = "app", businessType = BusinessType.IMPORT)
@PostMapping("/importData")
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
{
ExcelUtil<App> util = new ExcelUtil<>(App.class);
List<App> ac = util.importExcel(file.getInputStream());
String operName = getUsername();
String message = appService.importUser(ac, updateSupport, operName); // todo
return AjaxResult.success(message);
}
@PostMapping("/importTemplate")
public void importTemplate(HttpServletResponse response)
{
ExcelUtil<App> util = new ExcelUtil<>(App.class); // todo
util.importTemplateExcel(response, "地点");
}
后端Service层
/**
* 导入数据
*
* @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
* @param operName 操作用户
* @return 结果
*/
public String importUser(List<App> AC, Boolean isUpdateSupport, String operName);
后端Impl
/**
* 导入信息
*
* @param
* @return 结果
*/
@Override
public String importUser(List<App> AC, Boolean isUpdateSupport, String operName) {
if (StringUtils.isNull(AC) || AC.size() == 0){
throw new ServiceException("导入信息不能为空");
}
int successNum = 0;
int failureNum = 0;
Long n = 2023L;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
for(AppCanteen apps: AC) {
try {
String abc = app.getappName(); //拿到唯一的名称
appac = appMapper.selectAppCC(abc);
if (StringUtils.isNull(ac)) {
BeanValidators.validateWithException(validator, canteen);
apps.setCreateBy(operName);
apps.setPlayYear(n);
this.insertApp(apps);
successNum++;
successMsg.append("<br/>" + successNum + "、地点 " + app.getappName() + " 导入成功");
} else if (isUpdateSupport) {
BeanValidators.validateWithException(validator, canteen);
canteen.setCreateBy(operName);
this.updateAppCanteen(canteen);
successNum++;
successMsg.append("<br/>" + successNum + "、地点 " + app.getAppName() + " 更新成功");
} else {
failureNum++;
failureMsg.append("<br/>" + failureNum + "、地点 " + app.getAppName() + " 已存在");
}
} catch (Exception e) {
failureNum++;
String msg = "<br/>" + failureNum + "、地点 " + app.getAppName() + " 导入失败:";
failureMsg.append(msg);
}
}
if (failureNum > 0){
failureMsg.insert(0,"很抱歉导入失败 共" + failureNum +"条格式不对,错误如下");
throw new ServiceException(failureMsg.toString());
}else {
successMsg.insert(0,"共有"+ successNum + "导入成功");
}
return successMsg.toString();
}