easyexcel读取写入excel easyexceldemo

1.新建springboot项目

2.添加pom依赖

java 复制代码
<name>excel</name>
<description>excelspringboot例子</description>

<parent>
<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <apollo.version>2.3.0</apollo.version>
</properties>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>
    <dependency>
<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

    <dependency>
<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 其他依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
        <scope>provided</scope>
</dependency>

    <dependency>
<groupId>com.alibaba.fastjson2</groupId>
        <artifactId>fastjson2</artifactId>
        <version>2.0.51</version>
</dependency>
    <dependency>
<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.7.18</version>
</dependency>

<!--easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>3.2.1</version>
</dependency>


<!-- logback -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.17.1</version> <!-- 确保版本 >= 2.4 -->
</dependency>
    <dependency>
<groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.1</version> <!-- 确保版本 >= 2.4 -->
</dependency>


</dependencies>

<build>
<finalName>excel-demo</finalName>
    <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

新建实体类TestData

java 复制代码
package com.baozun.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

/**
 * 基础数据类
 *
 * @author Jiaju Zhuang
 **/
@Getter
@Setter
@EqualsAndHashCode
public class TestData {
    /**
     * 强制读取第三个 这里不建议 index 和 name 同时用,要么一个对象只用index,要么一个对象只用name去匹配
     */
    @ExcelProperty(index = 0)
    private String idOne;
    @ExcelProperty(index = 1)
    private String idTwo;
    @ExcelProperty(index = 2)
    private String idThree;
    @ExcelProperty(index = 3)
    private String idFour;
    @ExcelProperty(index = 4)
    private String idFive;
    @ExcelProperty(index = 5)
    private String idSix;

}
java 复制代码
新建TestDataListener

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson2.JSON;
import com.baozun.entity.TestData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**

* 模板的读取类
* 
* @author Jiaju Zhuang
  */
  @Slf4j
  public class TestDataListener extends AnalysisEventListener `<TestData>` {
  /**

  * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
    */
    private static final int BATCH_COUNT = 5;

  Set `<String>` firstColumnData = new HashSet<>();
  Set `<String>` secondColumnData = new HashSet<>();
  Set `<String>` differences = new HashSet<>();

  @Override
  public void invoke(TestData data, AnalysisContext context) {
  firstColumnData.add(data.getIdOne());
  secondColumnData.add(data.getIdTwo());

  }

  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
  // 找出两个列表的差异
  Set `<String>` set1 = new HashSet<>(secondColumnData);

  set1.removeAll(firstColumnData); // 在set1中但不在set2中的元素

  differences.addAll(set1);

  String fileName = "C:\\Users\\IdeaProjects\\exceldemo\\src\\main\\resources\\test.xlsx"; //需要提前新建目录
  // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
  EasyExcel.write(fileName, TestData.class).sheet("模板").doWrite(data());

  log.info("所有数据解析完成!");
  }

  private List `<TestData>` data(){
  List `<TestData>` list = new ArrayList<>();

  differences.forEach(item -> {
  TestData data = new TestData();
  data.setIdOne(item);
  list.add(data);});
  return list;
  }

新建test类ApplicationTests

java 复制代码
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baozun.ExcelMainApplication;
import com.baozun.entity.TestData;
import com.baozun.listener.TestDataListener;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest(classes = ExcelMainApplication.class)
class ApplicationTests {


    @Test
    void testEnrollCourse() {
        System.out.println("11");
    }

    /**
     * 写入xlsx文件测试方法
     */
    @Test
    public void simpleWriteXlsx() {
        String fileName = "C:\\Users\\IdeaProjects\\exceldemo\\src\\main\\resources\\test.xlsx"; //需要提前新建目录
        // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName, TestData.class).sheet("模板").doWrite(data());
    }

    /**
     * 写入xls文件测试方法
     */
    @Test
    public void simpleWriteXls() {

        String fileName = "C:\\Users\\IdeaProjects\\exceldemo\\src\\main\\resources\\test.xls";
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, TestData.class).excelType(ExcelTypeEnum.XLS).sheet("模板").doWrite(data());
    }


    /**
     * 读取xlsx文件最简单的读
     */
    @Test
    public void simpleReadXlsx() {

        String fileName = "C:\\Users\\IdeaProjects\\exceldemo\\src\\main\\resources\\testDifferData.xlsx";
        // 这里默认读取第一个sheet
        EasyExcel.read(fileName, TestData.class, new TestDataListener()).sheet().doRead();
    }

    /**
     * 读取xls文件最简单的读
     */
    @Test
    public void simpleReadXls() {

        String fileName = "d:/excel/simpleWrite.xls";
        EasyExcel.read(fileName, TestData.class, new TestDataListener()).excelType(ExcelTypeEnum.XLS).sheet().doRead();
    }


    private List<TestData> data(){
        List<TestData> list = new ArrayList<>();

        //算上标题,做多可写65536行
        //超出:java.lang.IllegalArgumentException: Invalid row number (65536) outside allowable range (0..65535)
        for (int i = 0; i < 2; i++) {
            TestData data = new TestData();
            data.setIdOne("11");
            data.setIdTwo("22");
            list.add(data);
        }
        return list;
    }

项目结构

相关推荐
CodeCraft Studio6 小时前
Excel处理控件Aspose.Cells教程:使用 Python 将 HTML 转换为 Excel
python·html·excel·aspose·aspose.cells·html转excel
Leo655356 小时前
Excel 读取阶段就去掉换行
excel
小镇学者7 小时前
【PHP】利用 xlswriter 扩展导出的Excel文件报错问题
php·excel
程序员杰哥8 小时前
Pytest与Unittest测试框架对比
自动化测试·软件测试·python·测试工具·测试用例·excel·pytest
cehuishi95278 小时前
excel中关联word邮件合并使用
word·excel·批量打印·邮件合并
曾令胜16 小时前
excel导出使用arthas动态追踪方法调用耗时后性能优化的过程
spring·性能优化·excel
我命由我1234519 小时前
Excel - Excel 列出一列中所有不重复数据
经验分享·学习·职场和发展·word·powerpoint·excel·职场发展
Lucky GGBond1 天前
Vue + Spring Boot 实现 Excel 导出实例
vue.js·spring boot·excel
缺点内向1 天前
C# 中 Excel 工作表打印前页面边距的设置方法
c#·.net·excel
极智-9961 天前
Excel如何合并单元格?【图文详解】Excel合并单元格技巧?单元格合并高阶操作?
excel·excel如何合并单元格·excel合并单元格技巧·单元格合并高阶操作·单元格合并·取消单元格合并·重复数据合并