在日常开发中,Excel文件的导出是一个非常常见的需求。阿里巴巴开源的EasyExcel框架因其轻量、高效和易于使用的特点,成为了许多开发者处理Excel数据的首选工具。然而,除了基本的数据读写功能外,如何通过样式设置让生成的表格更加美观、专业,也是提升用户体验的重要一环。
本文将详细解析EasyExcel中的样式设置方法,并结合实际案例演示如何让你的Excel表格更美观。
EasyExcel提供了多种方式来设置单元格样式,主要包括以下几种:
WriteHandler
接口实现高级样式控制。EasyExcel允许我们通过ExcelWriterProperties
对象设置全局样式。例如,我们可以统一设置字体、边框、背景色等。
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.AbstractCellWriteHandler;
import org.apache.poi.ss.usermodel.*;
import java.util.List;
public class GlobalStyleExample {
public static void main(String[] args) {
String fileName = "global_style_example.xlsx";
// 设置全局样式
ExcelWriterProperties properties = new ExcelWriterProperties();
properties.setOnlyAlias(true); // 使用别名
Workbook workbook = new XSSFWorkbook();
CellStyle globalCellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 12);
globalCellStyle.setFont(font);
properties.setGlobalCellStyle(globalCellStyle);
// 写入数据
EasyExcel.write(fileName, DemoData.class)
.withTemplate("template.xlsx")
.withWriteHandler(new AbstractCellWriteHandler() {
@Override
public void cell(Cell cell, WriteHandlerContext context) {
if (cell != null && cell.getCellType() == CellType.STRING) {
cell.setCellStyle(globalCellStyle);
}
}
})
.sheet("Sheet1")
.doWrite(data());
}
private static List<DemoData> data() {
List<DemoData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
}
class DemoData {
private String string;
private Date date;
private Double doubleData;
// Getters and Setters
}
在上述代码中,我们通过ExcelWriterProperties
设置了全局字体样式为Arial,字号为12。
列级样式设置可以通过@Column
注解实现。我们可以为不同的列指定不同的样式。
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
@ContentRowHeight(20)
@HeadRowHeight(30)
@ColumnWidth(25)
public class ColumnStyleExample {
@ExcelProperty("字符串标题")
private String string;
@ExcelProperty("日期标题")
@ColumnWidth(30) // 设置该列宽度为30
private Date date;
@ExcelProperty("数字标题")
@ColumnWidth(15) // 设置该列宽度为15
private Double doubleData;
// Getters and Setters
}
在上述代码中,我们通过@ColumnWidth
注解为不同列设置了不同的宽度,并且通过@ContentRowHeight
和@HeadRowHeight
注解调整了内容行和表头行的高度。
行级样式设置通常用于强调某些特殊行(如表头、合计行等)。我们可以通过实现WriteHandler
接口来自定义行级样式。
import com.alibaba.excel.write.handler.WriteHandler;
import org.apache.poi.ss.usermodel.*;
public class RowStyleHandler implements WriteHandler {
@Override
public void sheet(Sheet sheet, WriteHandlerContext context) {
// 设置整个工作表的样式
sheet.setDefaultColumnWidth(20);
}
@Override
public void row(Row row, WriteHandlerContext context) {
if (row.getRowNum() % 2 == 0) { // 偶数行设置背景色
CellStyle style = context.getWriteWorkbookHolder().getWriteWorkbook().createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
for (int i = 0; i < row.getLastCellNum(); i++) {
row.getCell(i).setCellStyle(style);
}
}
}
}
在上述代码中,我们实现了WriteHandler
接口,并在row
方法中为偶数行设置了黄色背景色。
如果需要更复杂的样式控制,可以通过实现AbstractCellWriteHandler
接口来自定义单元格样式。
import com.alibaba.excel.write.handler.CellWriteHandler;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
public class CustomCellStyleHandler extends AbstractCellWriteHandler {
@Override
public void afterCellDispose(Cell cell, WriteHandlerContext context) {
if (cell.getRowIndex() == 0) { // 表头样式
CellStyle headerStyle = context.getWriteWorkbookHolder().getWriteWorkbook().createCellStyle();
Font font = context.getWriteWorkbookHolder().getWriteWorkbook().createFont();
font.setBold(true);
font.setColor(IndexedColors.WHITE.getIndex());
headerStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerStyle.setBorderBottom(BorderStyle.THICK);
headerStyle.setBorderTop(BorderStyle.THICK);
headerStyle.setBorderLeft(BorderStyle.THICK);
headerStyle.setBorderRight(BorderStyle.THICK);
cell.setCellStyle(headerStyle);
} else { // 数据行样式
CellStyle dataStyle = context.getWriteWorkbookHolder().getWriteWorkbook().createCellStyle();
dataStyle.setBorderBottom(BorderStyle.THIN);
dataStyle.setBorderTop(BorderStyle.THIN);
dataStyle.setBorderLeft(BorderStyle.THIN);
dataStyle.setBorderRight(BorderStyle.THIN);
cell.setCellStyle(dataStyle);
}
}
}
在上述代码中,我们为表头设置了蓝色背景、白色字体,并添加了粗边框;为数据行设置了细边框。
以下是样式设置的整体流程图:
flowchart TD A[开始] --> B[创建ExcelWriter] B --> C[设置全局样式] C --> D[定义数据模型] D --> E[设置列级样式] E --> F[设置行级样式] F --> G[实现自定义样式] G --> H[写入数据并保存文件] H --> I[结束]
通过本文的介绍,我们可以看到EasyExcel不仅提供了强大的数据读写功能,还支持灵活多样的样式设置。无论是简单的全局样式调整,还是复杂的自定义样式控制,都可以轻松实现。合理运用这些样式设置技巧,可以显著提升生成Excel文件的专业性和美观度。