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

项目结构

相关推荐
测试老哥2 小时前
python+requests+excel 接口测试
自动化测试·软件测试·python·测试工具·测试用例·excel·接口测试
屹奕9 小时前
基于EasyExcel实现Excel导出功能
java·开发语言·spring boot·excel
我只会写Bug啊21 小时前
Vue文件预览终极方案:PNG/EXCEL/PDF/DOCX/OFD等10+格式一键渲染,开源即用!
前端·vue.js·pdf·excel·预览
谅望者1 天前
Linux文件查看命令完全指南:cat、less、head、tail、grep使用详解
linux·excel·less·shell·文件操作·命令行·系统运维
众纳2 天前
SpringBoot + EasyExcel 实现导入Excel并支持Excel中图片也能导入
excel
m5655bj2 天前
如何使用 Python 转换 Excel 工作表到 PDF 文档
开发语言·c#·excel
Eiceblue2 天前
使用 Java 将 Excel 工作表转换为 CSV 格式
java·intellij-idea·excel·myeclipse
Bianca4272 天前
Excel正则表达式.获取字符
正则表达式·excel
办公解码器2 天前
Excel怎么在下拉菜单中选择计算方式?
excel
梦里不知身是客112 天前
kettle的mysql 根据条件,导出到不同的excel中
数据库·mysql·excel