当前位置: 首页 > news >正文

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的执行过程

线程池的工作原理


 

相关文章:

  • Altair Radioss碰撞 安全与冲击 衡祖仿真
  • 鸿蒙应用开发(二)环境搭建
  • Java程序员,你掌握了多线程吗?
  • 第五节JavaScript typeof、类型转换与正则表达式
  • 解决POI导入内部错误方式
  • 实验01:静态路由配置实验
  • 最前端|Locofy试用报告:设计稿直接转换为代码
  • CanEasy多场景应用,让汽车总线测试更简单
  • JS对象笔记
  • Java八股文面试全套真题【含答案】- Redis篇
  • 迎接更高效的数据安全合规与风险评估,美创科技DCAS正式商用发布!
  • 一个简单的cmake模板(C++)
  • pytorch中的transpose用法
  • HDPE硅芯管强度高,抗压抗张和抗冲击强,外层不需其它套管
  • NFS远程文件共享系统!
  • k8s-learning-why we need pod
  • 学习git后,真正在项目中如何使用?
  • 李宏毅bert记录
  • 第三十章 控制到 XML 模式的映射 - Array of Classname
  • MySQL 报错 You can‘t specify target table for update in FROM clause解决办法
  • 习近平会见缅甸领导人敏昂莱
  • 上海楼市“银四”兑现:新房市场高端改善领跑,二手房量价企稳回升
  • 印度外交秘书:“朱砂行动”不针对军事设施,无意升级事态
  • 正荣地产:董事会主席、行政总裁辞任,拟投入更多精力推动境内债重组等工作
  • 全球第七个迪士尼主题公园将落户阿布扎比
  • 湖南张家界警方公告宣布一名外国人居留许可作废