easypoi 导出excel设置标题样式
程序员的公众号:源1024,获取更多资料,无加密无套路!
最近整理了一份大厂面试资料《史上最全大厂面试题》,Springboot、微服务、算法、数据结构、Zookeeper、Mybatis、Dubbo、linux、Kafka、Elasticsearch、数据库等等
获取方式: 关注公众号并回复 666 领取,更多内容持续奉上
基于easypoi的上一篇文章 easypoi-实现动态列导出excel
这次说说easypoi怎么设置标题的样式
直接上代码:
先看看源码ExportParams类中的默认style
public ExportParams() {
this.color = HSSFColorPredefined.WHITE.getIndex();
this.headerColor = HSSFColorPredefined.SKY_BLUE.getIndex();
this.type = ExcelType.XSSF;
this.style = ExcelExportStylerDefaultImpl.class;
this.headerHeight = 9.0D;
this.isCreateHeadRows = true;
this.isDynamicData = false;
this.isAppendGraph = true;
this.isFixedTitle = true;
this.maxNum = 0;
this.height = 0;
this.readonly = false;
this.autoSize = false;
}
跟踪代码:
this.style = ExcelExportStylerDefaultImpl.class;
public class ExcelExportStylerDefaultImpl extends AbstractExcelExportStyler implements IExcelExportStyler {
public ExcelExportStylerDefaultImpl(Workbook workbook) {
super.createStyles(workbook);
}
public CellStyle getTitleStyle(short color) {
CellStyle titleStyle = this.workbook.createCellStyle();
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle.setWrapText(true);
return titleStyle;
}
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setDataFormat(STRING_FORMAT);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
public CellStyle getHeaderStyle(short color) {
CellStyle titleStyle = this.workbook.createCellStyle();
Font font = this.workbook.createFont();
font.setFontHeightInPoints((short)12);
titleStyle.setFont(font);
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return titleStyle;
}
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setDataFormat(STRING_FORMAT);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
}
基于默认实现,我们可以重新定义IExcelExportStyler的实现:
ExcelExportStyle类
public class ExcelExportStyle implements IExcelExportStyler {
private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
private static final short FONT_SIZE_ELEVEN = 11;
private static final short FONT_SIZE_TWELVE = 12;
/**
* 标题样式
*/
private CellStyle headerStyle;
/**
* 每列标题样式
*/
private CellStyle titleStyle;
/**
* 数据行样式
*/
private CellStyle styles;
public ExcelExportStyle(Workbook workbook) {
this.init(workbook);
}
private void init(Workbook workbook) {
this.headerStyle = initHeaderStyle(workbook);
this.titleStyle = initTitleStyle(workbook);
this.styles = initStyles(workbook);
}
private CellStyle initHeaderStyle(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, (short) 16, true));
return style;
}
private CellStyle initTitleStyle(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
style.setDataFormat(STRING_FORMAT);
return style;
}
private CellStyle initStyles(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false));
style.setDataFormat(STRING_FORMAT);
return style;
}
private CellStyle getBaseCellStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
//水平居中
style.setAlignment(HorizontalAlignment.CENTER);
//上下居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
//设置自动换行
style.setWrapText(true);
return style;
}
private Font getFont(Workbook workbook, short size, boolean isBold) {
Font font = workbook.createFont();
//字体样式
font.setFontName("宋体");
//是否加粗
font.setBold(isBold);
//字体大小
font.setFontHeightInPoints(size);
return font;
}
@Override
public CellStyle getHeaderStyle(short i) {
return headerStyle;
}
@Override
public CellStyle getTitleStyle(short i) {
return titleStyle;
}
@Override
public CellStyle getStyles(boolean b, ExcelExportEntity excelExportEntity) {
return styles;
}
@Override
public CellStyle getStyles(Cell cell, int i, ExcelExportEntity excelExportEntity, Object o, Object o1) {
return getStyles(true, excelExportEntity);
}
@Override
public CellStyle getTemplateStyles(boolean b, ExcelForEachParams excelForEachParams) {
return null;
}
}
导出方法设置样式:
ExportParams exportParams = new ExportParams();
exportParams.setStyle(ExcelExportStyle.class);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, DataInfo.class, dataList);
效果
系列文章索引
MyBatis的插件能在哪些地方进行拦截?
了解MyBatis的缓存机制吗
面试官:谈谈对volatile的理解
Spring中用到了哪些设计模式
面试官:说一下SQL的执行过程
线程池的工作原理