SpringBoot使用EasyExcel将Excel数据直接转换为类对象
背景
相比于读取excel到List<List<String>>对象中,抽象一个方法将excel数据直接一步读取到指定的类对象中,更为方便。
代码
通过类Class读取excel数据到对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /** * 使用Class来读取Excel * * @param inputStream Excel的输入流 * @param excelTypeEnum Excel的格式(XLS或XLSX) * @return 返回 ClassList 的列表 */ public static <T> List<T> readExcelConvertObjectList(InputStream inputStream, ExcelTypeEnum excelTypeEnum, Class<T> classT) { return readExcelWithClassList(inputStream, excelTypeEnum, 1 , classT); } /** * 读取excel数据到数据对象 * * @param inputStream 文件流 * @param excelTypeEnum 文件类型Excel的格式(XLS或XLSX) * @param headLineNum 开始读取数据的行 * @param classT 转为对象的CLASS * @param <T> * @return */ public static <T> List<T> readExcelConvertObjectList(InputStream inputStream, ExcelTypeEnum excelTypeEnum, @Nullable Integer headLineNum, Class<T> classT) { if (headLineNum == null ) { headLineNum = 1 ; } return EasyExcel.read(inputStream).excelType(excelTypeEnum) .registerConverter( new StringConverter()) .head(classT) .sheet() .headRowNumber(headLineNum) .doReadSync(); } |
StringConvert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.property.ExcelContentProperty; public class StringConverter implements Converter<String> { @Override public Class supportJavaTypeKey() { return String. class ; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } /** * 将excel对象转成Java对象,这里读的时候会调用 * * @param cellData NotNull * @param contentProperty Nullable * @param globalConfiguration NotNull * @return */ @Override public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { return cellData.getStringValue(); } /** * 将Java对象转成String对象,写出的时候调用 * * @param value * @param contentProperty * @param globalConfiguration * @return */ @Override public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { return new CellData(value); } } |
使用时创建对应Excel的Java对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Data public class BankInfoImportExcelDto { @ExcelProperty (index = 0 ,value = "银行ID" ) @Min (value = 1 message = "ID不能小于1" ) private Long bankId; @ExcelProperty (index = 1 ,value = "银行名称" ) @NotBlank private String bankName; @ExcelProperty (index = 2 ,value = "银行地址" ) private String address; } |
编写Controller
1 2 3 4 5 6 7 8 | @PostMapping ( "/import" ) public ResultT importExcel( @RequestParam ( "file" ) MultipartFile file) throws IOException { InputStream inputStream = file.getInputStream(); List<BankInfoImportExcelDto> importBankInfoExcelDtoList = readExcelConvertObjectList(inputStream,ExcelTypeEnum.XLSX,BankInfoImportExcelDto. class ); ...... } |
JSR303注解
JSR-303 是 JAVA EE 6 中的一项子规范,叫做 Bean Validation。HibernateValidator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint。
Bean Validation 中内置的 constraint
Constraint | 详细信息 |
---|---|
@Null | 被注释的元素必须为 null |
@NotNull | 被注释的元素必须不为 null |
@AssertTrue | 被注释的元素必须为 true |
@AssertFalse | 被注释的元素必须为 false |
@Min(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
@Max(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
@DecimalMin(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
@DecimalMax(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
@Size(max, min) | 被注释的元素的大小必须在指定的范围内 |
@Digits (integer, fraction) | 被注释的元素必须是一个数字,其值必须在可接受的范围内 |
@Past | 被注释的元素必须是一个过去的日期 |
@Future | 被注释的元素必须是一个将来的日期 |
@Pattern(value) | 被注释的元素必须符合指定的正则表达式 |
Hibernate Validator 附加的 constraint
Constraint | 详细信息 |
---|---|
被注释的元素必须是电子邮箱地址 | |
@Length | 被注释的字符串的大小必须在指定的范围内 |
@NotEmpty | 被注释的字符串的必须非空 |
@Range | 被注释的元素必须在合适的范围内 |
相关文章:
https://www.dekux.com/post/32a50c31.html
https://cloud.tencent.com/developer/article/1888727
https://blog.csdn.net/qq_53121751/article/details/125301777
本篇文章如有帮助到您,请给「翎野君」点个赞,感谢您的支持。
出处:http://www.cnblogs.com/lingyejun/
若本文如对您有帮助,不妨点击一下右下角的【推荐】。
如果您喜欢或希望看到更多我的文章,可扫描二维码关注我的微信公众号《翎野君》。
转载文章请务必保留出处和署名,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通