poi - 常用单元格样式

这是 4.1.2 版本的写法,poi 版本差异较大,按需调整。

import org.apache.poi.ss.usermodel.*;

/**
 * Excel 常用样式参考
 * <p>
 * 很复杂的样式,可以使用 Excel 模版,从现有的 Excel 中直接读取样式,
 * 平时很少写这些样式,留作参考。
 *
 * @author Mr.css
 * @version 2023-09-15 9:05
 */
public class Test {

    /**
     * 使用代码直接设置样式
     *
     * @param workbook -
     * @return -
     */
    private CellStyle getStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();

        // 设置底边框;
        style.setBorderBottom(BorderStyle.THIN);
        // 设置底边框颜色;
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        // 设置左边框;
        style.setBorderLeft(BorderStyle.THIN);
        // 设置左边框颜色;
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        // 设置右边框;
        style.setBorderRight(BorderStyle.THIN);
        // 设置右边框颜色;
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        // 设置顶边框;
        style.setBorderTop(BorderStyle.THIN);
        // 设置顶边框颜色;
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());

        // 设置字体
        Font font = workbook.createFont();
        // 设置字体大小
        font.setFontHeightInPoints((short) 10);
        // 设置字体名字
        font.setFontName("微软雅黑");
        // 在样式用应用设置的字体;
        style.setFont(font);

        // 设置自动换行;
        style.setWrapText(false);
        // 设置水平对齐的样式为居中对齐;
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        // 设置填充样式(实心填充)
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 设置单元格颜色
        style.setFillForegroundColor(IndexedColors.LIME.getIndex());
        return style;
    }
}

posted on   疯狂的妞妞  阅读(58)  评论(0编辑  收藏  举报

(评论功能已被禁用)
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示