easypoi make easier import excel

1, did not talk much, in-line theme, how to solve a few steps excel import, is so easy.

First, import pom file.

<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-base</artifactId>
	<version>3.0.3</version>
</dependency>
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-web</artifactId>
	<version>3.0.3</version>
</dependency>

Two, Controller layer using ExcelImportUtil introduced.

@ApiOperation("导入excel")
@PostMapping("sc/importExcel")
@LogOperation(plate = "StandardCommissionController_importExcel", function = "配置管理-标准佣金配置-导入excel")
public ResultBean<Void> importExcel(UserDTO userDTO, @RequestParam("file") MultipartFile file) throws Exception {

	InputStream inputStream = null;
	try {
		inputStream = file.getInputStream();
		ImportParams params = new ImportParams();
		params.setNeedVerfiy(true);
		ExcelImportResult<StandardCommissionExcelVO> result = null;
		try {
			result = ExcelImportUtil.importExcelMore(inputStream, StandardCommissionExcelVO.class, params);
		} catch (Exception e) {
				throw new BussnisException(StateCommonCode.COMMON_PARAM_ERROR,
						"合同类型,业务类型,产品类型,产品情况,业务情况均只能是指定的中文值,请参考标准佣金新增下拉值");
			}
			List<StandardCommissionExcelVO> failList = result.getFailList();
			if (failList != null && failList.size() > 0) {
				StringBuffer sb = new StringBuffer("错误信息:");
				for (StandardCommissionExcelVO v : failList) {
					sb.append("【" + v.getErrorMsg() + "】;");
				}
				throw new BussnisException(StateCommonCode.COMMON_PARAM_ERROR, sb.toString());
			}
			List<StandardCommissionExcelVO> list = result.getList();
			standardCommissionService.saveExcel(list, userDTO);
			return ResultBean.toSuccessBean();
		} catch (IOException e) {
			e.printStackTrace();
			throw new BussnisException(StateCommonCode.COMMON_SYSTEM_ERROR, "系统IO流异常");
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
		}
	}

Third, the entity class, to achieve a IExcelModel, define a errorMsg error message string and setting get, set method.
@Data
public class StandardCommissionExcelVO the implements IExcelModel {

private String errorMsg;

@NotNull(message="年度不能为空")
@Excel(name = "年度", orderNum = "1", width = 4)
@Range(min = 1970, max = 2999, message = "年度输入错误,格式举例:2019")
private String year;

@NotNull(message="合同类型不能为空")
@Excel(name = "合同类型", orderNum = "2", replace = { "团购_1", "购销_2", "战略合作_3", "装修_4" })
private Integer contractType;

@NotNull(message="业务类型不能为空")
@Excel(name = "业务类型", orderNum = "3", replace = { "精装_1", "毛坯_2", "焕新家_3", "定制精装_4" })
private Integer businessType;

@NotNull(message="产品类型不能为空")
@Excel(name = "产品类型", orderNum = "4", replace = { "整装_1", "非整装_2" })
private Integer productType;

@NotBlank(message="产品情况名称不能为空")
@Excel(name = "产品情况", orderNum = "5")
private String productSituationName;

@NotNull(message="业务情况不能为空")
@Excel(name = "业务情况", orderNum = "6", replace = { "保底_1", "资源使用费_2", "无_3", "保底且资源使用费_4" })
private Integer businessSituation;

@NotBlank(message="配置项不能为空")
@Excel(name = "配置项", orderNum = "7")
private String configValue;

@NotNull(message="标准佣金(%)不能为空")
@Excel(name = "标准佣金(%)", orderNum = "8")
@Range(min = 1, max = 99, message = "标准佣金范围为1~99")
private Integer standardCommission;

@Override
public String getErrorMsg() {

	return errorMsg;
}

@Override
public void setErrorMsg(String errorMsg) {

	this.errorMsg = errorMsg;
}

}
Fourth, the service layer and then this collection object code to implement your logic, it's that simple.

V. debugging by postman.
Here Insert Picture Description

Published 10 original articles · won praise 0 · Views 357

Guess you like

Origin blog.csdn.net/weixin_43137113/article/details/104948472