easypoi 导入excel
- maven
- 工具类
- Excel实体类(注意这里注解中的值对应 Excel表格中列名)
- 调用样例
maven
cn.afterturn
easypoi-spring-boot-starter
4.2.0
cn.afterturn
easypoi-base
4.2.0
cn.afterturn
easypoi-web
4.2.0
cn.afterturn
easypoi-annotation
4.2.0
`
工具类
public class ExcelUtils {
private static ExcelUtils excelUtils;
//表格标题行数
private static Integer HEAD_ROWS = 0;
//表头行数
private static Integer TITLE_ROWS = 1;
private static boolean HEED_VERIFY = true;
public ExcelUtils() {
}
public static ExcelUtils getExcelUtils(){
return ExcelUtils.excelUtilsModel();
}
public static ExcelUtils getExcelUtils(Integer headRows, Integer titleRows){
HEAD_ROWS = headRows;
TITLE_ROWS = titleRows;
return ExcelUtils.excelUtilsModel();
}
public static ExcelUtils getExcelUtils(Integer headRows, Integer titleRows, boolean needVerify){
HEAD_ROWS = headRows;
TITLE_ROWS = titleRows;
HEED_VERIFY = needVerify;
return ExcelUtils.excelUtilsModel();
}
private static ExcelUtils excelUtilsModel(){
synchronized (ExcelUtils.class){
if (null == excelUtils){
return new ExcelUtils();
}
}
return excelUtils;
}
public static List importExcel(MultipartFile file, Class tClass) throws Exception {
if (file == null) {
throw new BusinessException("上传文件不能为空");
}
importParams importParams = new importParams();
// 数据处理
//表格标题行数,默认0
importParams.setHeadRows(HEAD_ROWS);
//表头行数,默认1
importParams.setTitleRows(TITLE_ROWS);
//是否需要校验上传的Excel,默认false
importParams.setNeedVerify(HEED_VERIFY);
ExcelimportResult resultModel = ExcelimportUtil.importExcelMore(file.getInputStream(), tClass, importParams);
if (null == resultModel)
throw new BusinessException("Excel读取失败");
return resultModel.getList();
}
}
Excel实体类(注意这里注解中的值对应 Excel表格中列名)
@Data
public class ExcelOrganDto implements Serializable {
private static final long serialVersionUID = 7451809392043549314L;
private String errorMsg;
@Excel(name = "机构名称")
private String organName;
@Excel(name = "机构级别(请选择)")
private String organLevelName;
@Excel(name = "机构类型(请选择)")
private String organTypeName;
@Excel(name = "行政上级机构(请填写)")
private String organParent;
@Excel(name = "证明下发上级机构(无办证资质不填)")
private String organParentIdProved;
@Excel(name = "证明审批上级机构(无办证资质不填)")
private String organParentIdApproval;
@Excel(name = "新筛管理上级机构(无新筛资质不填)")
private String organParentIdScreen;
@Excel(name = "所在乡镇")
private String townName;
@Excel(name = "乡镇编码")
private Long townCode;
@Excel(name = "所属村")
private String villageName;
@Excel(name = "村编码")
private Long villageCode;
@Excel(name = "管辖区域")
private String jurisdictionAreaName;
@Excel(name = "联系人(管理员)")
private String contact;
@Excel(name = "联系人电话")
private String contactMobile;
}
调用样例
MultipartFile file = null;
//返回的为读取到的信息 这里注意ExcelOrganDto 中的注解
//这里.getExcelUtils(1, 1) 中 参数为标题以及列名所在的行数 这里根据表格自动调整
List resultList = ExcelUtils.getExcelUtils(1, 1).importExcel(file, ExcelOrganDto.class);