当前位置:   article > 正文

java poi excel 设置单元格富文本 粗体无效解决办法_poi 设置富文本

poi 设置富文本

//富文本样式(一个单元格含有的多种样式格式)设置粗体无效解决办法 //设置RichTextString 的时候,给 font 设置 setBold(true) ; setItalic(true) 粗体 斜体 ,发现在office有效果,在wps 看不到加粗斜体效果。 //原因是而程序生成的加粗是<b val="true"/>电脑的生成的xlsx的加粗是<b/>,所以解决办法是把 val="true"属性抹去。效果如下

 直接上代码

Row row0 = sheet1.getRow(0);
row0.setHeightInPoints(19.5f);//行高设置
Cell titleCellStyleCell = row0.createCell(1);

CellStyle titleCellStyle = workbook.createCellStyle();//样式
titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平居中
titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直对齐

StringBuffer titleMsgSBF = new StringBuffer();
titleMsgSBF.append("下划线且加粗");
int title_length1 = titleMsgSBF.length();
titleMsgSBF.append("只加粗");
int title_length2 = titleMsgSBF.length();

//富文本样式(一个单元格含有的多种样式格式)
XSSFRichTextString hssfRichTextString = new XSSFRichTextString(titleMsgSBF.toString());
//设置单元格样式
Font title_length1_font = workbook.createFont(); // 创建字体样式
title_length1_font.setUnderline(Font.U_SINGLE);//下划线
title_length1_font.setBold(true); //字体加粗:true
title_length1_font.setFontName("Times New Roman"); // 设置字体类型
title_length1_font.setFontHeightInPoints((short) 15); // 设置字体大小
hssfRichTextString.applyFont(0, title_length1, title_length1_font);

//设置单元格样式
Font title_length2_font = workbook.createFont(); // 创建字体样式
title_length2_font.setBold(true); //字体加粗:true
title_length2_font.setFontName("宋体"); // 设置字体类型
title_length2_font.setFontHeightInPoints((short) 15); // 设置字体大小
hssfRichTextString.applyFont(title_length1, title_length2, title_length2_font);

//富文本样式(一个单元格含有的多种样式格式)设置粗体无效解决办法
//设置RichTextString 的时候,给 font 设置  setBold(true) ; setItalic(true)  粗体 斜体 ,发现在office有效果,在wps 看不到加粗斜体效果。
//原因是而程序生成的加粗是<b val="true"/>电脑的生成的xlsx的加粗是<b/>,所以解决办法是把 val="true"属性抹去
List<CTRElt> ctrEltList = hssfRichTextString.getCTRst().getRList();
if(ctrEltList != null && ctrEltList.size()>0){
    for(CTRElt ctrElt : ctrEltList){
        CTRPrElt ctrPrElt = ctrElt.getRPr();//获取属性元素
        if(ctrPrElt != null){
            List<CTBooleanProperty> bList = ctrPrElt.getBList();//获取<b/>元素
            if(bList != null && bList.size() > 0){
                bList.get(bList.size() - 1).unsetVal();//销毁设置的属性值
            }
        }
    }
}

titleCellStyleCell.setCellStyle(titleCellStyle);
titleCellStyleCell.setCellValue(hssfRichTextString);
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/889563
推荐阅读
相关标签
  

闽ICP备14008679号