需求:导入xls 文件 需要判断是否空值,时间格式是否问题
监听器
class ExcelListener extends AnalysisEventListener<RevWaterUserDocAndUserPayImportExl> {
public ExcelListener(List<RevWaterUserDocAndUserPayImportExl> result) {
this.list = result;
}
private List<RevWaterUserDocAndUserPayImportExl> list;
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
super.onException(exception, context);
}
@Override
public void invoke(RevWaterUserDocAndUserPayImportExl dto, AnalysisContext analysisContext) {
ReadRowHolder readRowHolder = analysisContext.readRowHolder();
Integer rowIndex = readRowHolder.getRowIndex(); // 行数
// 这里可以处理你的数据,例如校验字段是否为空
if (ObjectUtil.isNotNull(dto)) {
String meterCode = dto.getMeterCode();// 水表号
if (StringUtils.isEmpty(meterCode)) {
throw new IllegalArgumentException("第" + rowIndex + "行水表号未填写!");
}
String meterTypeName = dto.getMeterTypeName();// 水表类型
if (StringUtils.isEmpty(meterTypeName)) {
throw new IllegalArgumentException("第" + rowIndex + "行水表类型未填写!");
}
String userCode = dto.getUserCode();// 用户号
if (StringUtils.isEmpty(userCode)) {
throw new IllegalArgumentException("第" + rowIndex + "行用户号未填写!");
}
String userName = dto.getUserName();// 用户名
if (StringUtils.isEmpty(userName)) {
throw new IllegalArgumentException("第" + rowIndex + "行用户名未填写!");
}
String divisionName = dto.getDivisionName();// 所在行政区划
if (StringUtils.isEmpty(divisionName)) {
throw new IllegalArgumentException("第" + rowIndex + "行所在行政区划未填写!");
}
Long serialNumber = dto.getSerialNumber();// 序号
if (ObjectUtil.isNull(serialNumber)) {
throw new IllegalArgumentException("第" + rowIndex + "行序号未填写!");
}
String meterReaderName = dto.getMeterReaderName();// 抄表员
if (StringUtils.isEmpty(meterReaderName)) {
throw new IllegalArgumentException("第" + rowIndex + "行抄表员未填写!");
}
Double lastEndCode = dto.getLastEndCode();// 上月止码
if (ObjectUtil.isNull(lastEndCode)) {
throw new IllegalArgumentException("第" + rowIndex + "行上月止码未填写!");
}
Double currEndCode = dto.getCurrEndCode();// 本月止码
if (ObjectUtil.isNull(currEndCode)) {
throw new IllegalArgumentException("第" + rowIndex + "行本月止码未填写!");
}
Double unitPrice = dto.getUnitPrice();// 单价
if (ObjectUtil.isNull(unitPrice)) {
throw new IllegalArgumentException("第" + rowIndex + "行单价未填写!");
}
Double lastArrears = dto.getLastArrears();// 上月总欠
if (ObjectUtil.isNull(lastArrears)) {
throw new IllegalArgumentException("第" + rowIndex + "行上月总欠未填写!");
}
Double currArrears = dto.getCurrArrears();// 本月总欠
if (ObjectUtil.isNull(currArrears)) {
throw new IllegalArgumentException("第" + rowIndex + "行本月总欠未填写!");
}
Double payNeed = dto.getPayNeed();// 金额
if (ObjectUtil.isNull(payNeed)) {
throw new IllegalArgumentException("第" + rowIndex + "行金额未填写!");
}
Double totalUsage = dto.getTotalUsage();// 实用吨数
if (ObjectUtil.isNull(totalUsage)) {
throw new IllegalArgumentException("第" + rowIndex + "行实用吨数未填写!");
}
String paytime = dto.getPaytime();// 日期
if (StringUtils.isEmpty(paytime)) {
throw new IllegalArgumentException("第" + rowIndex + "行日期未填写!");
} else { // 时间转换
try {
Date date = DateUtil.parse(paytime);
} catch (DateException e) {
throw new IllegalArgumentException("第" + rowIndex + "行日期格式错误!");
}
}
}
}
@Override
public void extra(CellExtra extra, AnalysisContext context) {
super.extra(extra, context);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 解析完之后的逻辑
}
@Override
public boolean hasNext(AnalysisContext context) {
return super.hasNext(context);
}
}
调用监听器
@Transactional(rollbackFor = Exception.class)
public void importUserDocAndUser(MultipartFile file, String bilitime) throws IOException {
List<RevWaterUserDocAndUserPayImportExl> list = new ArrayList<>();
List<RevWaterUserDocAndUserPayImportExl> revWaterUserDocImpExlList = EasyExcel.read(file.getInputStream(),
new ExcelListener(list)).head(RevWaterUserDocAndUserPayImportExl.class).headRowNumber(1).sheet().doReadSync();
if (revWaterUserDocImpExlList.size() >= 10000) {
throw new RuntimeException("数据量大,单次批量导入超过10000条");
}
// 其他逻辑
}
标签:dto,未填写,rowIndex,EasyExcel,IllegalArgumentException,throw,new,xls,监听 From: https://www.cnblogs.com/lcaiqin/p/18323442