java读取excel通用方法
2021/12/20 14:49:32
本文主要是介绍java读取excel通用方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
java通用excel读取
- 只需要传入excel和对应的实体类即可实现自动解析
只需要传入excel和对应的实体类即可实现自动解析
1.由于是顺序解析class类的各字段所以需要 excel列的顺序和实体类中字段的顺序一致和类型对应
实体类代码省略,解析excel代码如下:
public static <T> List<T> parseExcel(InputStream fileInputStream, Class<T> type) throws IOException, InstantiationException, IllegalAccessException, InvalidFormatException { Workbook workBook = WorkbookFactory.create(fileInputStream); List<T> result = new ArrayList<>(); Sheet sheet = workBook.getSheetAt(0); for(int ri = sheet.getFirstRowNum() + 1 ; ri <= sheet.getLastRowNum(); ri++) { Row row = sheet.getRow(ri); if (row == null) { break; } T object = type.newInstance(); Field[] fields = object.getClass().getDeclaredFields(); Field.setAccessible(fields, true); boolean hasData = false; for(int ci = row.getFirstCellNum(); ci < row.getLastCellNum(); ci++) { Cell cell = row.getCell(ci); Object value = null; try { value = getCellValue(cell, fields[ci]); } catch (Exception e) { throw new RuntimeException("第" + (ri + 1) + "行第" + (ci + 1) + "列单元格数据解析异常"); } fields[ci].set(object, value); if (value != null) { hasData = true; } } if (hasData) { ValidatorFactory validatorFactory=Validation.buildDefaultValidatorFactory(); // 对对象属性进行校验 如果有问题 则直接抛出异常 Set<ConstraintViolation<T>> errorMsg = validatorFactory.getValidator().validate(object); if (!CollectionUtils.isEmpty(errorMsg)) { String failMsg = "第" + (ri + 1) + "行" + errorMsg.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(",")); throw new RuntimeException(failMsg); } result.add(object); } Field.setAccessible(fields, false); } return result; } /** * 读取单元格数据 可以根据需要补充其他类型与java类型的映射 * @param cell * @param field * @return */ private static Object getCellValue(Cell cell, Field field) { if(cell==null){ return null; } CellType type = cell.getCellTypeEnum(); if (CellType.BLANK == type) { return null; } else if (CellType.BOOLEAN == type && field.getType().equals(Boolean.class)) { return cell.getBooleanCellValue(); } else if (CellType.ERROR == type) { return null; } else if (CellType.NUMERIC == type || CellType.FORMULA == type) { if (field.getType().equals(Integer.class)) { return (int) cell.getNumericCellValue(); } else if (field.getType().equals(Long.class)) { return (long) cell.getNumericCellValue(); } else if (field.getType().equals(Double.class)) { return cell.getNumericCellValue(); } } else if (CellType.STRING == type && field.getType().equals(String.class)) { return cell.getStringCellValue(); } return null; }
调用时需要传excel对应的iNputstream和解析的实体类Class即可拿到对应的实体List;
如有侵权请联系删除!!!!!!!
这篇关于java读取excel通用方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)