easy poi 导入导出使用
导入实体类:
@Data
@ExcelTarget(“ProjectImport”)
public class ProjectImport implements IExcelModel {
//implements IExcelModel 自动校验需实现,错误信息存放在 errorMsg
/**
* 错误信息
*/
private String errorMsg;
/**
* 序号
*/
@ApiModelProperty(value = "序号")
@Excel(name = "序号*",**isImportField="true"**) // isImportField="true" 校验模板中是否有此字段,从而可以校验模板准确性。官方文档说默认为true,实际默认为false
@NotNull(message = "序号不能为空") //字段 非空校验
private Integer sort;
/**
* 项目所在开发区
*/
@ApiModelProperty(value = "项目所在开发区")
@Excel(name = "项目所在开发区",isImportField="true")
private String developmentIdDic;
/**
* 项目所在市、县(区)
*/
@ApiModelProperty(value = "项目所在市、县(区)")
@Excel(name = "项目所在市、县(区)",isImportField="true")//name 要和表格名称一致
private String areaIdDic;
/**
* 投资商集合
*/
@ExcelCollection(name="投资各方") //一对多,‘’投资各方’ 是 多的一方的一级表头
private List<InvestmentImport> investmentImportList;
}
导入模板示例:
导入逻辑代码
@PostMapping("/importProject")
@ApiOperation(“导入项目Excelssssss”)
public R importProjectss(@RequestParam(“file”) MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response, @ApiIgnore @LoginUser(isFull = true) SysUser sysUser) throws Exception {
//文件名
String originalFilename = multipartFile.getOriginalFilename();
if (!originalFilename.endsWith(“xls”) && !originalFilename.endsWith(“xlsx”)) {
return R.fail(“导入文件只支持xls/xlsx格式!”);
}
ImportParams params = new ImportParams();
params.setTitleRows(2); //这是标题,excel的,表头以上全是标题 params.setHeadRows(2);//这是表头,名称/介绍之类的
params.setNeedVerify(true); //开启校验
params.setVerifyHandler(new aaa());//自定义校验
ExcelImportResult<ProjectImport> list = new ExcelImportResult(); //解析excel返回结果,包含两个list:成功的一个list,失败的一个list
try {
list = ExcelImportUtil.importExcelMore(multipartFile.getInputStream(), ProjectImport.class, params);
} catch (Exception e) {
e.printStackTrace();
return R.fail(e.getMessage());
}
//数据之间的逻辑校验,可以在setVerifyHandler中完成,本项目逻辑复杂,所以单独写了方法校验
List<Project> projectList = new ArrayList<>();
//校验通过的
List<Project> projectLists = projectService.importDataHandle(list.getList(), sysUser);
//检验失败的
List<Project> projectListt = projectService.importDataHandle(list.getFailList(), sysUser);
projectList.addAll(projectLists);
projectList.addAll(projectListt);
String error = "";
for (Project project : projectList) {
if (!org.springframework.util.StringUtils.isEmpty(project.getErrorMsg())) {
error += project.getErrorMsg() + " ";
}
}
if (!StringUtils.isEmpty(error)) {
return R.fail(error);
}
for (Project project : projectList) {
projectService.saveProject(project, sysUser);
}
return R.success("导入成功");
}
导出功能
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProjectExcel {
/**
* 序号
*/
@Excel(name = "序号",width = 6,height = 21,orderNum ="1")
private Integer sort;
/**
* 项目名称
*/
@Excel(name = "项目名称",width = 15,height = 21,orderNum ="2")
private String projectName;
/**
* 项目描述
*/
@Excel(name = "项目概述",width = 25,height = 21,orderNum ="3")
private String description;
/**
* 项目投资各方
*/
@Excel(name = "项目投资各方",width = 15,height = 21,orderNum ="4")
private String investor;
/**
* 投资总额(亿元)
*/
@Excel(name = "投资总额(亿元)",width = 15,height = 21,orderNum ="5")
private Double amount;
/**
* 合同引资额(亿元)
*/
@Excel(name = "合同引资额(亿元)",width = 15,height = 21,orderNum ="7")
private Double contractInvestmentAmount;
/**
* 行业分类
*/
@Excel(name = "行业分类", width = 15, height = 21,orderNum = "8")
private String category;
/**
* 合作方式
*/
@Excel(name = "合作方式",width = 15,height = 21,orderNum ="9")
private String cooperationStyle;
/**
* 投资方是否为世界500强
*/
@Excel(name = "投资方是否为世界500强",width = 15,height = 21,orderNum ="10")
private String yes;
/**
* 投资方是否为中国制造业企业100强
*/
@Excel(name = "投资方是否为中国制造业企业100强",width = 15,height = 21,orderNum ="11")
private String yesOne;
/**
* 投资方是否为中国民营企业100强
*/
@Excel(name = "投资方是否为中国民营企业100强",width = 15,height = 21,orderNum ="11")
private String yesTwo;
/**
* 是否属于增资项目
*/
@Excel(name = "是否属于增资项目",width = 15,height = 21,orderNum ="12")
private String yesThree;
/**
* 是否属于徽商回归项目
*/
@Excel(name = "是否属于徽商回归项目",width = 15,height = 21,orderNum ="13")
private String yesFour;
/**
* 联系人
*/
private String person;
/**
* 手机
*/
private String phone;
/**
* 联系人及手机
*/
@Excel(name = "联系人及手机",width = 15,height = 21,orderNum ="13")
private String peopleAndPhone;
/**
* 央企/民企/外企/港澳企/台企/侨企
*/
@Excel(name = "央企/民企/外企/港澳企/台企/侨企",width = 15,height = 21,orderNum ="13")
private String enterprise;
/**
* 项目所在开发区
*/
@Excel(name = "项目所在开发区",width = 15,height = 21,orderNum ="13")
private String developmentZone;
/**
* 项目所在市、县(区)
*/
@Excel(name = "项目所在市、县(区)", width = 15, height = 21, orderNum = "13")
private String city;
/**
* id
*/
@ExcelIgnore
private String id;
}
public static void exportExcel(List<?> list, String title, String sheetName, ProjectExcel pojoClass, String fileName, HttpServletResponse response) {
//list 导出的数据 pojoClass 数据的类型 fileName文件名称
// sheetName sheet名 title 标题
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}