官方文档:https://www.yuque.com/easyexcel/doc/write#W4u1e
项目结构
导入pom依赖
<?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.zkd</groupId>
<artifactId>poi</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.7</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>provided</scope>
</dependency>
<!-- logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<!--日期格式化工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
写入Excel
创建DemoData类
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.util.Date;
@Data
public class DemoData {
//自定义标题
@ExcelProperty("姓名")
private String name;
@ExcelProperty("日期")
private Date date;
@ExcelProperty("年龄")
private Integer age;
}
测试代码:
import com.alibaba.excel.EasyExcel;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Text {
//文件生成路径
String path="D:\\idea_gzkj\\Easyexcel\\";
private List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>();
//写数据
DemoData data = new DemoData();
data.setName("张三");
data.setDate(new Date());
data.setAge(18);
list.add(data);
return list;
}
/**
* 最简单的写
*/
@Test
public void simpleWrite() {
//文件名称
String fileName = path+"EasyText.xlsx";
//写
EasyExcel.write(fileName, DemoData.class).sheet("表名称").doWrite(data());
System.out.println("写入完成");
}
}
效果:
读取Excel
创建解析器
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
public class DemoDataListener extends AnalysisEventListener<DemoData> {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
// invokeHeadMap解析表头数据
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap));
}
// invoke解析表头一下的数据
@Override
public void invoke(DemoData data, AnalysisContext context) {
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
LOGGER.info("所有数据解析完成!");
}
}
测试代码
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
@Slf4j
public class Text {
//文件路径
String path="D:\\idea_gzkj\\Easyexcel\\";
@Test
public void simpleRead() {
//文件路径
String fileName = path + "EasyText.xlsx";
//读
EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();
}
}
效果:
在解析器中调用service方法
需要加上
@AllArgsConstructor 注解