easypoi导入复杂表头Excel
EasyPoi导入Excel
·
引入Easypoi的maven坐标,我使用的是4.0.0版本
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
先看下Excel模板
实体类
package com.lee.entity.excel;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.handler.inter.IExcelDataModel;
import cn.afterturn.easypoi.handler.inter.IExcelModel;
import lombok.Data;
import javax.validation.constraints.NotBlank;
/**
* @author lwh
* @date 2022/7/26
* @description IExcelDataModel 获取导入正常的接口,IExcelModel 获取导入异常的接口
**/
@Data
public class CompetitorInputEntity implements IExcelDataModel, IExcelModel {
/**
* 行号
*/
private int rowNum;
/**
* 错误消息
*/
private String errorMsg;
@Excel(name = "厂家名称", fixedIndex = 0)
@NotBlank(message = "厂家名称,不能为空")
private String factory;
@Excel(name = "产品品牌", fixedIndex = 1)
@NotBlank(message = "产品品牌,不能为空")
private String brand;
@Excel(name = "规格", fixedIndex = 2)
@NotBlank(message = "规格,不能为空")
private String tireSpecName;
@Excel(name = "层级", fixedIndex = 3)
@NotBlank(message = "层级,不能为空")
private String tireLayerName;
@Excel(name = "花纹", fixedIndex = 4)
private String tireFigureName;
@Excel(name = "花纹深度(mm)", fixedIndex = 5)
private Double figureDepth;
@Excel(name = "胎面宽(mm)", fixedIndex = 6)
private Double tireWidth;
@Excel(name = "外胎重量(kg)", fixedIndex = 7)
private Double tireWeight;
@Excel(name = "市场批发价格", fixedIndex = 8)
private Double tireMarketWholesalePrice;
@Excel(name = "经销商到底价", fixedIndex = 9)
private Double tireDistributorFinallyPrice;
@Excel(name = "市场批发价格", fixedIndex = 10)
private Double setMarketWholesalePrice;
@Excel(name = "经销商到底价", fixedIndex = 11)
private Double setDistributorFinallyPrice;
@Excel(name = "系列", fixedIndex = 12)
private String zhongceSeriesName;
@Excel(name = "规格", fixedIndex = 13)
private String zhongceTireSpecName;
@Excel(name = "层级", fixedIndex = 14)
private String zhongceTireLayerName;
@Excel(name = "花纹", fixedIndex = 15)
private String zhongceTireFigureName;
@Excel(name = "负荷指数", fixedIndex = 16)
private String loadIndex;
@Excel(name = "速度级别", fixedIndex = 17)
private String speedLevel;
@Excel(name = "特殊三包政策", fixedIndex = 18)
private String specialAssurePolicy;
@Excel(name = "竞品产品优劣势分析及建议", fixedIndex = 19)
private String remark;
@Excel(name = "车型", fixedIndex = 20)
private String carType;
@Excel(name = "马力(匹)", fixedIndex = 21)
private Double horsepower;
/**
* 路况(1=长途高速、2=普通国道、3=城市道路、4=矿山路面、5=其他)
*/
@Excel(name = "路况", fixedIndex = 22)
private String roadCondition;
/**
* 最低载重量
*/
@Excel(name = "最小", fixedIndex = 23)
private Double loadWeightLeft;
/**
* 最高载重量
*/
@Excel(name = "最大", fixedIndex = 24)
private Double loadWeightRight;
/**
* 最低车速
*/
@Excel(name = "最小", fixedIndex = 25)
private Double speedLeft;
/**
* 最高车速
*/
@Excel(name = "最大", fixedIndex = 26)
private Double speedRight;
/**
* 最低运输距离
*/
@Excel(name = "最小", fixedIndex = 27)
private Double runDistanceLeft;
/**
* 最高运输距离
*/
@Excel(name = "最大", fixedIndex = 28)
private Double runDistanceRight;
}
我看网上对于复杂表头的导入要使用groupName,我没有使用也没有什么问题,应该是后续版本升级了,去掉了繁琐操作,但是fixedIndex 是一定要写对的,否则映射不上数据。
导入的代码
@ApiOperation("EasyPoi导入数据测试")
@PostMapping("/importCompetitorData")
public Boolean importCompetitorData(@RequestParam("file") MultipartFile file) throws Exception {
ImportParams params = new ImportParams();
// 表头设置为3行
params.setHeadRows(3);
// 标题行设置为0行,默认是0,可以不设置
params.setTitleRows(1);
// 是否需要校验上传的Excel,默认为false
params.setNeedVerify(true);
ExcelImportResult<CompetitorInputEntity> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
CompetitorInputEntity.class, params);
System.out.println("是否校验失败: " + result.isVerfiyFail());
System.out.println("校验失败的集合:" + JSONObject.toJSONString(result.getFailList()));
System.out.println("校验通过的集合:" + JSONObject.toJSONString(result.getList()));
for (CompetitorInputEntity entity : result.getFailList()) {
String msg = "第" + (entity.getRowNum() + 1) + "行的错误是:" + entity.getErrorMsg();
System.out.println(msg);
}
return true;
}
TitleRows 和 HeadRows 如何设置?
TitleRows: 代表标题行,可以理解忽略的行数,在导入的过程中忽略掉。
HeadRows:代表表头,默认是1,但是对比我这次的导入的Excel,我设置的表头是3。
还不懂?来幅图,是不是瞬间发出,噢~~
测试下
第6行厂家名称我没填,验证下检验怎么样,看样子没问题。
白嫖到这,还不赶紧收藏
更多推荐
已为社区贡献1条内容
所有评论(0)