温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中怎么将Excel读取成List对象数组

发布时间:2021-06-30 17:51:29 阅读:1055 作者:Leah 栏目:编程语言
Java开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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>
   

2.2 根据文件流创建一个Workbook

可以使用WorkbookFactory自动根据Excel类型是XLSX还是XLS自动创建对应的Workbook

File file = new File(filePath + File.separator + fileName);FileInputStream inputStream = new FileInputStream(file);// 使用工厂模式 根据文件扩展名 创建对应的WorkbookWorkbook workbook = WorkbookFactory.create(inputStream);
   

2.3 获取确定sheet页的数据

根据 sheet 页的名字获取 sheet 页数据

Sheet sheet = workbook.getSheet(sheetName);
   

2.4 获取总的数据行数

Sheet 类提供了获取首行行号和最后一行行号的方法,可以根据这两个方法获取 sheet 页中的数据行数。

int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
   

2.5 获取行列数据封装到Map中

如果只需要返回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());}
   

2.6 将结果数据转成List对象

使用 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元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/2366803/blog/5024028

AI

开发者交流群×