在实际的工作中,我们经常会遇到数据的导入和导出,而常用的方式就是对Excel表格数据的操作,下面简单介绍一下如何用fastexcel快速读、写Excel
1、引入相关依赖
<dependency>
<groupId>cn.idev.excel</groupId>
<artifactId>fastexcel</artifactId>
<version>1.0.0</version>
</dependency>
这里使用的是fastexcel,它是由easyExcel的原作者在之前的基础之上再次开源的一个操作Excel的工具,API和之前的EasyExcel大同小异基本兼容,只是包不一样而已
2、读Excel
2.1、定义和Excel数据对应的一个JavaBean
import cn.idev.excel.annotation.ExcelProperty;
import cn.idev.excel.annotation.format.DateTimeFormat;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable {
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "性别")
private String gender;
@ExcelProperty(value = "年龄")
private Integer age;
@ExcelProperty(value = "生日")
@DateTimeFormat(value = "yyyy-MM-dd")
@JSONField(format = "yyyy-MM-dd")
private Date birthday;
}
我这里为了偷懒,项目引入了lombok,省去了生成getter、setter方法。
@ExcelProperty注解对应Excel的表头,具体我的Excel文件内容如下:
2.2、定义一个解析器去处理自己的业务逻辑
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON;
import com.demo.fastexcel.beans.Student;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class StudentParseListener implements ReadListener<Student> {
@Override
public void invoke(Student student, AnalysisContext analysisContext) {
log.info("解析一条学生信息:{}", JSON.toJSONString(student));
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("解析完毕");
}
}
我这里是demo,没什么业务逻辑可言,只是简单地打印了一下
2.3、正式读
public class FastexcelDemoApplication {
public static void main(String[] args) {
String fileName = "/Users/navy/data/students.xlsx";
// 读取 Excel 文件
FastExcel.read(fileName, Student.class, new StudentParseListener()).sheet().doRead();
}
}
可以看到,就这么水灵灵地将Excel的数据读取到了
3、写Excel
3.1、简单写
如果只是简单地写,那非常简单,只需要像这样一行代码就可以把数据写到Excel里了
public class FastexcelDemoApplication {
public static void main(String[] args) {
String fileName = "/Users/navy/data/students.xlsx";
List<Student> students = new ArrayList<>();
students.add(new Student("张三丰", "男", 20, new Date()));
students.add(new Student("张无忌", "女", 21, new Date()));
students.add(new Student("张岁三", "男", 23, new Date()));
students.add(new Student("张毅", "男", 19, new Date()));
students.add(new Student("张天师", "男", 29, new Date()));
FastExcel.write(fileName).sheet("student").head(Student.class).doWrite(students);
}
}
3.2、模板写
fastexcel除了可以简单地写文件之外,有时候我们还需要设置一些导出文件的格式,如果我们要在写数据的时候一个单元格一个单元格处理就比较麻烦,这个时候可以利用模板去写。
3.2.1、准备模板
首先我们创建一个模板
注意:
① 花括号里边是我们定义的变量,如{total}就是我们往map里塞的total。
② 花括号里边加了个点(.),表示我们传的是一个列表,括号里边的值从list里的元素里找对应的值,如
.name
就是从我们下面students列表里边的Student里找对应的name
3.2.2、写文件
public class FastexcelDemoApplication {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("张三丰", "男", 20, new Date()));
students.add(new Student("张无忌", "女", 21, new Date()));
students.add(new Student("张岁三", "男", 23, new Date()));
students.add(new Student("张毅", "男", 19, new Date()));
students.add(new Student("张天师", "男", 29, new Date()));
//写模板
String template = "/Users/navy/data/学生模板.xlsx";
String stuFile = "/Users/navy/data/学生信息.xlsx";
try (ExcelWriter excelWriter = FastExcel.write(stuFile).withTemplate(template).build();){
WriteSheet writeSheet = EasyExcel.writerSheet().build();
//填充模板第一部分的列表数据
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
excelWriter.fill(students, fillConfig, writeSheet);
//填充列表下面的total
Map<String, Object> extraData = new HashMap<>();
extraData.put("total", students.size());
excelWriter.fill(extraData, writeSheet);
}
}
}
执行程序之后,可以看到对应的文件已经自动生成,并且数据已经按照模板格式写进去了