Java中怎么将Excel读取成List对象数组,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
导入POM包
<!--POI 包--><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.versin}</version></dependency><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.versin}</version></dependency>
可以使用WorkbookFactory
自动根据Excel类型是XLSX还是XLS自动创建对应的Workbook
File file = new File(filePath + File.separator + fileName);FileInputStream inputStream = new FileInputStream(file);// 使用工厂模式 根据文件扩展名 创建对应的WorkbookWorkbook workbook = WorkbookFactory.create(inputStream);
根据 sheet 页的名字获取 sheet 页数据
Sheet sheet = workbook.getSheet(sheetName);
Sheet 类提供了获取首行行号和最后一行行号的方法,可以根据这两个方法获取 sheet 页中的数据行数。
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
如果只需要返回Map数据,到这里就可以返回结果
for (int i = 1; i < rowCount + 1; i++) { Row row = sheet.getRow(i); resultMap = new HashMap<>(); for (int j = 0; j < row.getLastCellNum(); j++) { if(Objects.equals(row.getCell(j).getCellType(), CellType.STRING)) { resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getStringCellValue()); } else if(Objects.equals(row.getCell(j).getCellType(), CellType.NUMERIC)) { resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getNumericCellValue()); }else { resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j)); } } jsonObject = new JSONObject(resultMap); resultMapList.add(jsonObject.toJSONString());}
使用 fasterxml.jackson
将Map结果数据转成 List 对象
return JsonUtil.ofList(resultMapList.toString(), tClass);
/** * 获取Excel,将数据转换成 List<T> 的形式 * Excel 数据要求第一行为对象的属性名称 * * @param filePath 文件路径 * @param fileName 文件名称 * @param sheetName sheet名称 * @param tClass 要转换成的实体类 * @param <T> * @return List对象数组 * @throws IOException */public static <T> List<T> readExcelOfList(String filePath, String fileName, String sheetName, Class<T> tClass) throws IOException { List<String> resultMapList = new ArrayList<>(); File file = new File(filePath + File.separator + fileName); FileInputStream inputStream = new FileInputStream(file); // 使用工厂模式 根据文件扩展名 创建对应的Workbook Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheet(sheetName); int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum(); JSONObject jsonObject; Map<String, Object> resultMap; for (int i = 1; i < rowCount + 1; i++) { Row row = sheet.getRow(i); resultMap = new HashMap<>(); for (int j = 0; j < row.getLastCellNum(); j++) { if(Objects.equals(row.getCell(j).getCellType(), CellType.STRING)) { resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getStringCellValue()); } else if(Objects.equals(row.getCell(j).getCellType(), CellType.NUMERIC)) { resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getNumericCellValue()); }else { resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j)); } } jsonObject = new JSONObject(resultMap); resultMapList.add(jsonObject.toJSONString()); } return JsonUtil.ofList(resultMapList.toString(), tClass);}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/2366803/blog/5024028