Apache POI 解析和处理Excel

摘要:由于开发需要批量导入Excel中的数据,使用了Apache POI库,记录下使用过程

 1. 背景  

        Java 中操作 Excel 文件的库常用的有Apache POI 和阿里巴巴的 EasyExcel 。Apache POI 是一个功能比较全面的 Java 库,适合处理复杂的 Office 文件操作需求;而 EasyExcel 则是一个专注于 Excel 文件读写的简单、高效工具,更适合处理 Excel 文件的批量读写需求,并且易于上手。当Excel的数据量很大时推荐使用EasyExcel更合适。

        Apache的POI 用于处理 Microsoft Office 格式文件(如Excel、Word、PowerPoint)。它支持读取、写入和操作 Excel 文件,可以处理各种 Excel 格式(如 .xls 和 .xlsx)。
Apache POI - the Java API for Microsoft Documentsicon-default.png?t=N7T8https://poi.apache.org/

        阿里的EasyExcel
关于Easyexcel | Easy ExcelEasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目,在尽可能节约内存的情况下支持读写百M的Excel。icon-default.png?t=N7T8https://easyexcel.opensource.alibaba.com/docs/current/

2. Apache POI的使用

2.1 引入依赖poi和poi-ooxml

        首先,在pom.xml中配置相关依赖,并使用Maven引入

<!--引入处理Excel的POI类
  poi:这是Apache POI的核心模块,用于处理Excel 97-2003格式的.xls 文件。
  poi-ooxml:这个模块则用于处理Office Open XML格式的.xlsx 文件,它是基于XML的Office文件格式,对应于较新版本的Excel。
-->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.16</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.16</version>
</dependency>
2.2 使用POI读取Excel中数据填充对象

        使用Apache POI解析Excel大体过程:通过输入流FileInputStream读取Excel中的数据,使用XSSFWorkbook类加载输入流,创建了一个新版本的Excel工作簿(下例子为workbook),从创建的工作簿中获取第一个工作表 firstsheet,再通过迭代器逐行逐个单元格遍历工作表,将单元格中的数据填充到对象中,并将对象添加到列表。此时,对得到的列表遍历,执行相应的数据库操作,实现批量导入的功能。

public void addProductByExcel(File destFile) throws IOException {
    // 从Excel文件中读取商品信息,并转换为商品列表
    List<Product> products = readProductsFromExcel(destFile);
    for (int i = 0; i < products.size(); i++) {
        Product product =  products.get(i);
        Product productOld = productMapper.selectByName(product.getName()); // 通过商品名称来查询数据库中是否已存在同名商品
        if (productOld != null) {  // 查询到同名商品,则抛出自定义异常
            throw  new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        int count = productMapper.insertSelective(product); // 不存在同名商品,则向数据库中插入当前商品信息,存储受影响的行数
        if (count == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }
    }
}

private List<Product> readProductsFromExcel(File excelFile) throws IOException {
    ArrayList<Product> listProducts = new ArrayList<>();  // 用于存储读取到的商品信息
    FileInputStream inputStream = new FileInputStream(excelFile);  // 文件输入流 inputStream,用于读取Excel文件中的数据

    XSSFWorkbook workbook = new XSSFWorkbook(inputStream); // 使用XSSFWorkbook类加载输入流,创建了一个新版本的Excel工作簿workbook
    XSSFSheet firstsheet =  workbook.getSheetAt(0); // 获取第一个工作表,index从0还是1开始需要根据实际情况确认
    Iterator<Row> iterator = firstsheet.iterator();  // 通过工作表的迭代器创建一个行迭代器iterator,用于逐行遍历工作表中的数据
    while(iterator.hasNext()) {
        Row nextRow = iterator.next(); // 逐行遍历
        Iterator<Cell> cellIterator = nextRow.cellIterator();  // 逐个单元格遍历
        Product aProduct = new Product();  // 创建Product对象aProduct,用于存储当前行数据对应的商品信息
        // 读取单元格填充aProduct
        while (cellIterator.hasNext()) {
            Cell nextCell = cellIterator.next();
            int columnIndex = nextCell.getColumnIndex(); //获取单元格的索引,即列号
            switch (columnIndex) {
                case 0:
                    aProduct.setName((String) ExcelUtil.getCellValue(nextCell)); // 针对不同的列号执行相应的操作,将单元格数据填充到aProduct对象的相应属性中
                    break;
                case 1:
                    aProduct.setImage((String) ExcelUtil.getCellValue(nextCell));
                    break;
                case 2:
                    aProduct.setDetail((String) ExcelUtil.getCellValue(nextCell));
                    break;
                case 3:
                    Double cellValue = (Double) ExcelUtil.getCellValue(nextCell); // excel中数字类型默认为Double
                    aProduct.setCategoryId(cellValue.intValue());
                    break;
                case 4:
                    cellValue = (Double) ExcelUtil.getCellValue(nextCell); // excel中数字类型默认为Double
                    aProduct.setPrice(cellValue.intValue());
                    break;
                case 5:
                    cellValue = (Double) ExcelUtil.getCellValue(nextCell); // excel中数字类型默认为Double
                    aProduct.setStock(cellValue.intValue());
                    break;
                case 6:
                    cellValue = (Double) ExcelUtil.getCellValue(nextCell); // excel中数字类型默认为Double
                    aProduct.setStatus(cellValue.intValue());
                    break;
                default:
                    break;
            }
        }
        listProducts.add(aProduct); // 将填充好数据的aProduct对象添加到商品列表listProducts中
    }
    workbook.close();  // 关闭Excel工作簿
    inputStream.close();  // 关闭文件输入流
    return listProducts;
}

        综上,实现了读取Excel中的数据,填充进入对象listProducts,最终添加到数据库的功能。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值