1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > easypoi导出excel不设置样式_EasyPOI 导出excel设置边框 背景颜色 字体样式

easypoi导出excel不设置样式_EasyPOI 导出excel设置边框 背景颜色 字体样式

时间:2024-02-09 12:15:24

相关推荐

easypoi导出excel不设置样式_EasyPOI 导出excel设置边框 背景颜色 字体样式

EasyPOI 导出excel设置边框,背景颜色,字体样式

EasyPOI 导出代码示例ExportParamsexportParams=newExportParams();

exportParams.setStyle(ExcelExportStyler.class);//设置样式

Workbookworkbook=ExcelExportUtil.exportExcel(exportParams,CallPoolExcelVo.class,voList);

其中 ExcelExportStyler 就是我的自定义样式类。

官方默认提供了一个样式枚局类 ExcelStyleType,但是在 4.2 的版本中,BORDER、COLOR 这两个已经被标记为淘汰,不建议继续使用。

官方地址介绍:/doc/easypoi.html#304publicenumExcelStyleType{

NONE("默认样式",ExcelExportStylerDefaultImpl.class),

BORDER("边框样式",ExcelExportStylerBorderImpl.class),

COLOR("间隔行样式",ExcelExportStylerColorImpl.class);

....

}

所以,我们可以继承 ExcelExportStylerDefaultImpl 或者实现 IExcelExportStyler 接口,完善其中方法即可。publicinterfaceIExcelExportStyler{

/**

*列表头样式

*@paramheaderColor

*@return

*/

publicCellStylegetHeaderStyle(shortheaderColor);

/**

*标题样式

*@paramcolor

*@return

*/

publicCellStylegetTitleStyle(shortcolor);

/**

*获取样式方法

*@paramParity

*@paramentity

*@return

*/

publicCellStylegetStyles(booleanParity,ExcelExportEntityentity);

}

ExcelExportStyler 源码

为阅读方便,请忽略尾行注释的规范。importcn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;

importcn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;

importcn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;

importorg.apache.poi.ss.usermodel.*;

publicclassExcelExportStylerimplementsIExcelExportStyler{

privatestaticfinalshortSTRING_FORMAT=(short)BuiltinFormats.getBuiltinFormat("TEXT");

privatestaticfinalshortFONT_SIZE_TEN=10;

privatestaticfinalshortFONT_SIZE_ELEVEN=11;

privatestaticfinalshortFONT_SIZE_TWELVE=12;

/**

*大标题样式

*/

privateCellStyleheaderStyle;

/**

*每列标题样式

*/

privateCellStyletitleStyle;

/**

*数据行样式

*/

privateCellStylestyles;

publicExcelExportStyler(Workbookworkbook){

this.init(workbook);

}

/**

*初始化样式

*

*@paramworkbook

*/

privatevoidinit(Workbookworkbook){

this.headerStyle=initHeaderStyle(workbook);

this.titleStyle=initTitleStyle(workbook);

}

/**

*大标题样式

*

*@paramcolor

*@return

*/

@Override

publicCellStylegetHeaderStyle(shortcolor){

returnheaderStyle;

}

/**

*每列标题样式

*

*@paramcolor

*@return

*/

@Override

publicCellStylegetTitleStyle(shortcolor){

returntitleStyle;

}

/**

*数据行样式

*

*@paramparity可以用来表示奇偶行

*@paramentity数据内容

*@return样式

*/

@Override

publicCellStylegetStyles(booleanparity,ExcelExportEntityentity){

returnstyles;

}

/**

*获取样式方法

*

*@paramdataRow数据行

*@paramobj对象

*@paramdata数据

*/

@Override

publicCellStylegetStyles(Cellcell,intdataRow,ExcelExportEntityentity,Objectobj,Objectdata){

returngetStyles(true,entity);

}

/**

*模板使用的样式设置

*/

@Override

publicCellStylegetTemplateStyles(booleanisSingle,ExcelForEachParamsexcelForEachParams){

returnnull;

}

/**

*初始化--大标题样式

*

*@paramworkbook

*@return

*/

privateCellStyleinitHeaderStyle(Workbookworkbook){

CellStylestyle=getBaseCellStyle(workbook);

style.setFont(getFont(workbook,FONT_SIZE_TWELVE,true));

returnstyle;

}

/**

*初始化--每列标题样式

*

*@paramworkbook

*@return

*/

privateCellStyleinitTitleStyle(Workbookworkbook){

CellStylestyle=getBaseCellStyle(workbook);

style.setFont(getFont(workbook,FONT_SIZE_ELEVEN,false));

//背景色

style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());

style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

returnstyle;

}

/**

*基础样式

*

*@return

*/

privateCellStylegetBaseCellStyle(Workbookworkbook){

CellStylestyle=workbook.createCellStyle();

style.setBorderBottom(BorderStyle.THIN);//下边框

style.setBorderLeft(BorderStyle.THIN);//左边框

style.setBorderTop(BorderStyle.THIN);//上边框

style.setBorderRight(BorderStyle.THIN);//右边框

style.setAlignment(HorizontalAlignment.CENTER);//水平居中

style.setVerticalAlignment(VerticalAlignment.CENTER);//上下居中

style.setWrapText(true);//设置自动换行

returnstyle;

}

/**

*字体样式

*

*@paramsize字体大小

*@paramisBold是否加粗

*@return

*/

privateFontgetFont(Workbookworkbook,shortsize,booleanisBold){

Fontfont=workbook.createFont();

font.setFontName("宋体");//字体样式

font.setBold(isBold);//是否加粗

font.setFontHeightInPoints(size);//字体大小

returnfont;

}

}

参考文档:/zhouqu3790/article/details/82688580

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。