EasyExcel基本使用

EasyExcel介绍

bash 复制代码
https://github.com/alibaba/easyexcel

示例

pom.xml

xml 复制代码
<?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.studio</groupId>
    <artifactId>TestEasyExcel</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.32</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>4.0.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

User.java

java 复制代码
package com.studio.pojo;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;


/**
 * 说明
 * 1)index:指定列所在的索引,从0开始
 * 2)@DateTimeFormat 自定义日期格式
 * 3)@ExcelIgnore 忽略指定表头信息
 * 4)合并表头
 * 5)设置单元格大小
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@HeadRowHeight(value = 25) // 表头行高
@ContentRowHeight(value = 25) // 内容行高
@ColumnWidth(value = 30) // 列宽
public class User implements Serializable {

    @ExcelProperty(value = {"用户基本信息", "用户名"}, index = 1)
    private String userName;
    @ExcelProperty(value = {"用户基本信息", "年龄"}, index = 2)
    private Integer age;
    @ExcelProperty(value = {"用户基本信息", "地址"}, index = 4)
    private String address;
    @ExcelProperty(value = {"用户基本信息", "生日"}, index = 3)
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    private Date birthday;


    @ExcelIgnore
    @ExcelProperty(value = {"用户基本信息", "用户ID"}, index = 5)
    private String userId;
}

Main.java

java 复制代码
package com.studio;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.studio.pojo.User;

import java.util.ArrayList;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
//        writeExcel();
        readExcel();
    }

    /**
     * 写入Excel
     */
    public static void writeExcel(){
        ArrayList<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setAddress("上海" + i);
            user.setUserName("张三" + i);
            user.setBirthday(new Date());
            user.setAge(10 + i);
            users.add(user);
        }
        EasyExcel.write("E:\\用户.xls", User.class).sheet("用户信息").doWrite(users);
    }

    /**
     * 读取Excel
     */
    public static void readExcel(){
        ArrayList<User> users = new ArrayList<>();
        //读取数据
        EasyExcel.read("E:\\用户.xls", User.class, new AnalysisEventListener<User>() {
            @Override
            public void invoke(User user, AnalysisContext analysisContext) {
                System.out.println("读取一行:" + user);
                users.add(user);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("所有读取完成");
            }
        }).sheet().doRead();
        System.out.println(users);
    }
}

效果

相关推荐
苏-言2 分钟前
Spring IOC实战指南:从零到一的构建过程
java·数据库·spring
界面开发小八哥9 分钟前
更高效的Java 23开发,IntelliJ IDEA助力全面升级
java·开发语言·ide·intellij-idea·开发工具
草莓base23 分钟前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Allen Bright36 分钟前
maven概述
java·maven
编程重生之路38 分钟前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
薯条不要番茄酱39 分钟前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
努力进修1 小时前
“探索Java List的无限可能:从基础到高级应用“
java·开发语言·list
politeboy1 小时前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
Daniel 大东2 小时前
BugJson因为json格式问题OOM怎么办
java·安全
Theodore_10226 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee