首页 > 解决方案 > 如何在 poi apache java 中同时设置单元格 fontHeight 9 和 Bold

问题描述

我正在使用 poi apache libraru 创建文件 Excel。在标题栏中,我想将字体高度设置为 9 并设置字体粗体。但我只能在上面的两件事中设置一件事。这是我的代码。你能帮助我吗。感谢

    XSSFCellStyle cellFont_9 = workbook.createCellStyle();
    cellFont_9.setFont(font_9);

    XSSFCellStyle cellFontBold = workbook.createCellStyle();
    cellFontBold.setFont(fontBold);

    setRowStyleExcell(workbook, headerRow, cellFont_9);
    setRowStyleExcell(workbook, headerRow, cellFontBold);

在这种情况下,我的字体是粗体

其他情况:

    XSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setFont(font_9);
    cellStyle.setFont(fontBold);
    setRowStyleExcell(workbook, headerRow, cellStyle);

它只是粗体

这是我的 setRowStyleExcell() 函数:

public void setRowStyleExcell(Workbook workbook, Row row, XSSFCellStyle style){
    for(int i = 0; i < row.getLastCellNum(); i++){
        //For each cell in the row
        if(row.getCell(i) != null){
            row.getCell(i).setCellStyle(style);//Set the style
        }
    }
}

标签: javaapache-poi

解决方案


您必须创建具有这两个属性的对象,Font然后设置FontXSSFCellStyle

在你的情况下

Font font = .... // Initialize it the way you do now
font.setBold(true); // Set bold
font.setFontHeight((short) 9); // Set font height
cellFont_9.setFont(font_9); // Set on XSSFCellStyle

推荐阅读