引入meaven包
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
<!--还需要引入poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
使用bean类
根据自己的需求创建对应的bean,这一类可以是根据数据库中的字段来定义。
我这里就用我数据库里面的一个表来做展示
数据库中的表
表中的数据
代码中的bean
package com.text;
import com.alibaba.excel.annotation.ExcelProperty;
import com.definesys.mpaas.query.annotation.Column;
import com.definesys.mpaas.query.annotation.Table;
/**
* @copyright: Shanghai Definesys Company.All rights reserved.
* @description:
* @author: 小黑
* @since: 2021/11/19 9:50
* @history: 1.2021/11/19 created by xiaohei
*/
@Table(value = "student")
public class Student {
@Column(value = "id")
@ExcelProperty("学生编号")
private String id;
@Column(value = "name")
@ExcelProperty("学生姓名")
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
将数据写入excle表格
这边的代码的目的就是把这些从数据库中获取到的数据,打包成对象然后插入到excle表格中。
Controller层
@RequestMapping(value = "/easyExcleTest",method = RequestMethod.POST)
public Response easyExcleTest(){
return Response.ok().data(autoCheckService.easyExcleTest());
}
Service层
public String easyExcleTest() {
return autoCheckDao.easyExcleTest();
}
Dao层
public String easyExcleTest() {
//先获取所有的数据
List<Student> students = sw.buildQuery().doQuery(Student.class);
//新建一个list
List<String> fileNames = new ArrayList<String>();
//用时间来区分文件信息,保证文件不会重复
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
//给文件取名
String fileName = "D:\\a\\文件"+format.format(date)+".xls";
//输出一次文件名
System.out.println(fileName);
//创建excle写入对象
ExcelWriter excelWriter = null;
//设置写入对象的名称为fileName
excelWriter = EasyExcel.write(fileName).build();
//设置sheet的名称
WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").head(Student.class).build();
//将我们上面获取到的students对象写入到sheet中。
excelWriter.write(students,writeSheet);
//将文件名称加入到fileNames列表中
fileNames.add(fileName);
//关闭写入流,一定要关,不能忘
excelWriter.finish();
//返回fileNames给接口调用。
return String.valueOf(fileNames);
}
使用上面的代码来进行接口调用测试
最后得到的数据表格的形式就是这样的:
如何对输出的excle表格进行格式的设置。
//设置头背景为蓝色
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND,fillForegroundColor = 152)
//设置头字体为16
@HeadFontStyle(fontHeightInPoints = 16)
//设置内容背景的颜色为浅蓝色
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND,fillForegroundColor = 155,horizontalAlignment= HorizontalAlignment.CENTER,borderBottom = BorderStyle.THIN,borderRight = BorderStyle.THIN)
//设置内容字体为10
@ContentFontStyle(fontHeightInPoints = 10)
//设置内容的高
@ContentRowHeight(30)
//设置头的高
@HeadRowHeight(40)
设置字体的颜色
这里就举例子成红色了哦
@Column(value = "id")
@ExcelProperty("学生编号")
@HeadFontStyle(fontHeightInPoints = 16,color = 5)//黄色的列表头
@ContentFontStyle(fontHeightInPoints = 10,color = 2)//红色的内容
private String id;
@Column(value = "name")
@ExcelProperty("学生姓名")
@HeadFontStyle(fontHeightInPoints = 16,color = 6)//粉色的列表头
@ContentFontStyle(fontHeightInPoints = 10,color = 3)//绿色的内容
private String name;
可以看到,这些都是可以单独进行设置颜色和格式的。
单独设置当前列的列宽
对于某一列的宽度进行设置,可以直接在对应的bean中的属性上面添加注解
@ColumnWidth(20)//设置列宽为20
隐藏字段
只需要在想要隐藏的字段上面添加这一条注解即可
@ExcelIgnore//隐藏字段
设置列所在的位置
字段对应的列的位置可以通过设置注解的属性来进行设置
@ExcelProperty(value = "应用/接口名",index = 1)
通过这个注解可以显示字段,也可以设置字段显示在excle表格中的名称
具体的详细的使用请参考官方文档
EasyExcle使用的官方文档.