java实现读取excel表格中的数据,兼容xls和xlsx

前言

利用 java 实现读取 excel 表格中的数据,兼容 xls 与 xlsx 格式,不用额外做区分,写不同的实现方法。

引入的依赖包

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>

java 代码实现

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yuhuofei
 * @version 1.0
 * @description 读取excel表格中的数据,并处理,兼容xls和xlsx
 * @date 2022/9/10 19:40
 */
public class ExcelUtil {

    public static List<List<String>> readExcel(String excelFilePath) {
        List<List<String>> result = new ArrayList<>();
        if (excelFilePath.endsWith(".xlsx") || excelFilePath.endsWith(".xls")) {
            result = handleData(excelFilePath);
        }
        return result;
    }

    //读取后缀是xlsx的excel表格
    public static List<List<String>> handleData(String excelFilePath) {
        List<List<String>> result = new ArrayList<>();
        try (InputStream inputStream = new FileInputStream(excelFilePath);
             Workbook xssfWorkbook = WorkbookFactory.create(inputStream)) {
            //循环每一页,并处理当前的循环页
            for (Sheet sheet : xssfWorkbook) {
                if (sheet == null) {
                    continue;
                }

                for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                    //Row表示每一行的数据
                    Row row = sheet.getRow(rowNum);
                    if (null != row) {
                        int minColIx = row.getFirstCellNum();
                        int maxColIx = row.getLastCellNum();
                        List<String> rowList = new ArrayList<>();
                        //遍历该行,并获取每一个cell的数据
                        for (int colIx = minColIx; colIx < maxColIx; colIx++) {
                            Cell cell = row.getCell(colIx);
                            if (cell == null) {
                                continue;
                            }
                            rowList.add(cell.toString());
                        }
                        result.add(rowList);
                    }
                }
            }
        } catch (IOException | InvalidFormatException e) {
            System.out.println(e);
        }
        return result;
    }

    public static void main(String[] args) {
        String path1 = "D:/demo/test/Desktop/test01.xls";
        List<List<String>> list1 = readExcel(path1);
        System.out.println(list1);

        String path2 = "D:/demo/test/Desktop/test02.xlsx";
        List<List<String>> list2 = readExcel(path2);
        System.out.println(list2);
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值