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

项目结构

相关推荐
Dxy123931021613 小时前
Python如何合并两个Excel文件
爬虫·python·excel
wtsolutions2 天前
Batch Conversion Online JSON Files (from URL) to Excel by WTSolutions
json·excel·batch
码尚云标签2 天前
导入Excel打印
excel·excel导入·标签打印软件·打印知识·excel导入打印教程
lilv662 天前
python中用xlrd、xlwt读取和写入Excel中的日期值
开发语言·python·excel
大虫小呓3 天前
14天搞定Excel公式:告别加班,效率翻倍!
excel·excel 公式
瓶子xf3 天前
EXCEL-业绩、目标、达成、同比、环比一图呈现
excel
码尚云标签3 天前
批量打印Excel条形码
excel·标签打印·条码打印·一维码打印·条码批量打印·标签打印软件·打印教程
Wangsk1334 天前
用 Python 批量处理 Excel:从重复值清洗到数据可视化
python·信息可视化·excel·pandas
叶甯4 天前
【Excel】vlookup使用小结
excel
AI手记叨叨4 天前
Python分块读取大型Excel文件
python·excel