一、EasyExcel 简介
EasyExcel 是一个开源的跨平台 Java 处理 Excel 的第三方库,它采用了注解方式设置 Excel 表格的属性,可以轻松实现 Excel 的读写、导入导出等操作。EasyExcel 支持 XLS 和 XLSX 两种 Excel 格式,并且使用效率高、易于使用、具有完善的文档和示例支持。
二、EasyExcel 导出的优点
相比于 POI,EasyExcel 导出的优点在于如下几点:
- 采用注解方式设置 Excel 表格的属性,实现代码的可读性和可维护性;
- 采用流式写入方式,大幅度减少内存占用,提升导出效率;
- 支持多线程导出,进一步提升导出效率;
- 支持直接导出到 FTP、SFTP、HTTP 等网络存储中;
- 可以轻松实现对特殊的格式(如日期格式、货币格式等)进行设置;
- 具有完善的文档和示例支持,容易上手。
三、EasyExcel 导出的基本使用方法
1. 导出简单 Excel 表格
下面的示例演示了如何使用 EasyExcel 导出一个简单的 Excel 表格:
public class ExportDemo {
@Test
public void simpleExport() {
// 1. 创建需要导出的 Excel 对象
ExcelWriter excelWriter = EasyExcel.write("demo.xlsx").build();
// 2. 定义导出的表格的表头和数据集
Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("sheet1");
List<List<Object>> data = new ArrayList<>();
List<Object> head = new ArrayList<>();
head.add("第一列");
head.add("第二列");
data.add(head);
for(int i = 0; i < 10; i++) {
List<Object> tmpData = new ArrayList<>();
tmpData.add("列1的数据:" + i);
tmpData.add("列2的数据:" + i);
data.add(tmpData);
}
// 3. 导出表格
excelWriter.write(data, sheet1);
excelWriter.finish();
}
}
以上代码实现了导出一个简单的 Excel 表格,通过 ExcelWriter 对象的 write()
方法将数据集写入到表格中即可完成导出。
2. 导出复杂 Excel 表格
EasyExcel 可以很方便地导出带有样式、图片、超链接等复杂 Excel 表格。下面的示例演示了如何使用 EasyExcel 导出带有图片、样式的 Excel 表格:
public class ExportDemo {
@Test
public void complexExport() throws IOException {
// 1. 创建需要导出的 Excel 对象
String fileName = "demo.xlsx";
OutputStream out = new FileOutputStream(fileName);
ExcelWriter excelWriter = EasyExcel.write(out).build();
// 2. 定义导出的表格的表头和数据集
Sheet sheet = new Sheet(1, 0);
sheet.setSheetName("sheet1");
List<List<Object>> data = initData();
sheet.setHead(initHead());
sheet.setAutoWidth(Boolean.TRUE);
// 3. 导出图片到磁盘并且插入到表格中
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File("D://logo.png"));
ImageIO.write(bufferImg, "png", byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
Image image = new Image(new ByteArrayInputStream(imageBytes), 5, 5, 3, 7);
sheet.addImage(image);
// 4. 导出表格
excelWriter.write(data, sheet);
excelWriter.finish();
}
private List<List<Object>> initData() {
List<List<Object>> dataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<Object> item = new ArrayList<>();
item.add(i);
item.add(i + " 的名称");
item.add(100 + i);
item.add("https://www.baidu.com");
item.add(new Date());
dataList.add(item);
}
return dataList;
}
private List<List<String>> initHead() {
List<List<String>> head = new ArrayList<>();
List<String> head0 = new ArrayList<>();
List<String> head1 = new ArrayList<>();
List<String> head2 = new ArrayList<>();
List<String> head3 = new ArrayList<>();
List<String> head4 = new ArrayList<>();
head0.add("编号");
head1.add("名称");
head2.add("价格");
head3.add("超链接");
head4.add("日期");
head.add(head0);
head.add(head1);
head.add(head2);
head.add(head3);
head.add(head4);
return head;
}
}
以上代码首先创建了一个 ExcelWriter 对象,然后定义表头和数据和图片等内容,在写入 Excel 表格时将这些内容一起写入即可。
3. 设置单元格样式
EasyExcel 可以非常方便地设置单元格样式,支持字体、背景颜色、粗体、斜体等样式设置。下面的示例演示了如何使用 EasyExcel 设置单元格的样式。
public class StyleDemo {
@Test
public void setStyle() throws IOException {
// 1. 创建需要导出的 Excel 对象
String fileName = "style.xlsx";
OutputStream out = new FileOutputStream(fileName);
ExcelWriter excelWriter = EasyExcel.write(out).build();
// 2. 定义导出的表格的表头和数据集
Sheet sheet = new Sheet(1, 0);
sheet.setSheetName("sheet1");
List<List<Object>> data = initData();
sheet.setHead(initHead());
sheet.setAutoWidth(Boolean.TRUE);
// 3. 设置单元格样式
WriteCellStyle headCellStyle = new WriteCellStyle();
headCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
WriteFont headFont = new WriteFont();
headFont.setFontHeightInPoints((short) 14);
headFont.setBold(true);
headCellStyle.setWriteFont(headFont);
headCellStyle.setShrinkToFit(true);
headCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 4. 导出表格
excelWriter.write(data, sheet, headCellStyle);
excelWriter.finish();
}
private List<List<Object>> initData() {
List<List<Object>> dataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<Object> item = new ArrayList<>();
item.add(i);
item.add(i + " 的名称");
item.add(100 + i);
item.add("https://www.baidu.com");
item.add(new Date());
dataList.add(item);
}
return dataList;
}
private List<List<String>> initHead() {
List<List<String>> head = new ArrayList<>();
List<String> head0 = new ArrayList<>();
List<String> head1 = new ArrayList<>();
List<String> head2 = new ArrayList<>();
List<String> head3 = new ArrayList<>();
List<String> head4 = new ArrayList<>();
head0.add("编号");
head1.add("名称");
head2.add("价格");
head3.add("超链接");
head4.add("日期");
head.add(head0);
head.add(head1);
head.add(head2);
head.add(head3);
head.add(head4);
return head;
}
}
以上代码使用 WriteCellStyle
和 WriteFont
分别设置单元格样式和字体样式,在写入 Excel 表格时将这些样式一起写入即可。
4. 导出大批量数据
EasyExcel 通过采用流式写入的方式,非常适合导出大批量数据,同时支持多线程导出,极大地提升了导出效率。下面的示例演示了如何使用 EasyExcel 导出大批量数据。
public class BigDataDemo {
@Test
public void bigDataExport() {
// 1. 创建需要导出的 Excel 对象
String fileName = "big_data.xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName).build();
// 2. 定义导出的表格的表头和数据集
Sheet sheet = new Sheet(1, 0);
sheet.setSheetName("Sheet1");
List<List<Object>> data = new ArrayList<>();
for (int i = 0; i < 100000; i++) {
List<Object> row = new ArrayList<>();
row.add(i);
row.add("名称 " + i);
row.add(new Date());
row.add(200 + i);
row.add("https://www.baidu.com");
data.add(row);
}
// 3. 导出表格
excelWriter.write(data, sheet);
excelWriter.finish();
}
}
以上代码中,我们演示了如何使用 EasyExcel 导出大批量数据。在生成大数据量时,我们需要注意减少内存占用,使用流式写入方式;并且针对不同需求,可以采用多线程导出的方式,提升导出效率。
四、总结
通过以上的介绍,我们可以看到 EasyExcel 导出在 Java 中是非常好用的一个开源库。它通过采用注解方式定义 Excel 表格属性和流式写入方式,极大地简化了 Excel 的导出操作,同时支持复杂的格式化与样式设置,对于多种需求场景也都有高效的解决方案。无论是在线上业务中,还是在数据分析处理中,EasyExcel 都是一个非常值得使用的工具。