2021-10-25excel表的导入导出,JXL、POI二种方法对数据进行读写。

这篇博客介绍了在项目中从使用jxl库处理xls格式Excel到因需求变更转而使用poi处理xlsx格式的过渡过程。作者详细分享了 Maven 项目配置,包括依赖管理,并提供了读写Excel的类和测试案例。文章强调了 poi 库在处理xlsx格式上的必要性,以及在日期格式处理上的一些挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

excel表xls、xlsx,jxl、poi操作

前言:
随着jxl停止更新,对excel的读写有了局限性,它无法对excel中xlsx格式进行操作。现在市面上大部分公司都用的都是poi它支持对xls、xlsx格式的excel表读写。小编刚刚入职到另一家公司,上来写的第一个就是excel数据表、模板表,数据整合到一起,要求数据表中有数据的字段与其对应的模板表数据字段对应,将数据导入模板表中。前期需求没有限制excel的格式,小编用的同一的格式xls,jxl对此是支持,操作起来也比较简单。后期需求改动要都是xlsx,那没办法只能重写了用的是poi。里里外外弄了10来天,所以这里小编把这2次的代码图发到这里分享给各位小伙伴。

步骤:
一、首先我们需要建一个maven项目,将同一版本号的依赖导入。(版本不同,容易导致运行失败。有点版本还支持对excel数据分类查询)
依赖呢,有些是多余的,试了太多种方法,用不到的依赖忘记清理了。(这2种方法都能用以下依赖)

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.ycq.online</groupId>
  <artifactId>excel-poi</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>excel-poi</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>


    <!--日期格式化工具-->
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.10.1</version>
    </dependency>


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

    <!--xlsx(07)-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.9</version>
    </dependency>


    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>easyexcel</artifactId>
      <version>2.1.4</version>
    </dependency>

    <!--mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.19</version>
    </dependency>


    

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <version>2.2.4.RELEASE</version>
      <scope>test</scope>
    </dependency>


    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-nop</artifactId>
      <version>1.7.25</version>
    </dependency>


    <dependency>
      <groupId>com.ycq.online</groupId>
      <artifactId>excel-poi</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
    </dependency>
      <dependency>
          <groupId>net.sourceforge.jexcelapi</groupId>
          <artifactId>jxl</artifactId>
          <version>2.6.12</version>
      </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.20</version>
    </dependency>
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.31</version>
    </dependency>
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.30</version>
    </dependency>
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.29</version>
    </dependency>
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <version>1.5.13</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

二、创建读存数据的ReadExcel类

在这里插入图片描述
jxl2003的ReadExcel类中readExcelA指的是数据表,readExcelA2模板表
在这里插入图片描述

在这里插入图片描述
三、测试类,将数据写入excel表中
在这里插入图片描述
总结:效果图有公司的数据,这里就不方便展示了。

POI2007xlsx格式

步骤 :
一、写ReadExcel类,都是ArrayList存储的,我这里读的话日期格式处理很麻烦,虽然最后成功了,但是控制台打印的结果不美观
在这里插入图片描述
二、写测试类
在这里插入图片描述

总结:这里用的都是ArrayList存储方式,对数据进行读和存的,xls格式下可以用jxl读,xlsx只能用poi去读,我这里用的都是String类型,去读取数据。希望我的方法对小伙伴们有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A smirK squirrel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值