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;
    }

项目结构

相关推荐
herinspace11 分钟前
管家婆iShop如何调整商品成本?
服务器·数据库·学习·电脑·excel
SunnyDays101116 分钟前
使用 Python 轻松操控 Excel 网格线:隐藏、显示与自定义颜色
开发语言·python·excel
青衫客3621 分钟前
浅谈 Apache POI:XSSFWorkbook 的原理与实践(Java 操作 Excel 实践指南)
java·apache·excel
未来之窗软件服务11 小时前
数据库(九)SQL 模式操作 Excel——东方仙盟练气
数据库·sql·excel·仙盟创梦ide·东方仙盟·数据库修复
缺点内向17 小时前
C#实战:使用Spire.XLS for .NET 将Excel转换为SVG图片
c#·自动化·.net·excel
C_心欲无痕1 天前
使用 XLSX.js 导出 Excel 文件
开发语言·javascript·excel
ekkcole1 天前
easyexcel2.2.10对本地文件的指定行或指定列添加样式
excel·easyexcel
城数派1 天前
1901-2024年我国省市县三级逐年潜在蒸散发数据(Shp/Excel格式)
excel
门思科技1 天前
ThinkLink批量操作功能详解:如何通过Excel高效管理物联网设备与配置
物联网·excel
scx_link2 天前
使用 Excel 中的 “快速填充“
excel