Setting a part of the cell contents to bold using apache poi?

前端 未结 3 1196
悲&欢浪女
悲&欢浪女 2021-01-11 14:08

How can i put this string in to a cell using java apache poi?
The string is \"Hello world Hello\"
As u can see i need to make a part of the text bol

相关标签:
3条回答
  • 2021-01-11 14:19
    cell.setValue(value);
    RichTextString rts = cell.getRichStringCellValue();
    rts.applyFont(value.indexOf(subValue), value.length(), boldFonts);
    cell.setCellValue(rts);
    

    Where value=something+subValue. Here subValue contents will be with bold fonts. for example if we have someting="Hello" and subValue="world" we will print:

    Hello world

    in the cell.

    0 讨论(0)
  • 2021-01-11 14:27

    This will print "Hello world Hello" in a cell

    XSSFRichTextString rts= new XSSFRichTextString("Hello ");
    
    XSSFFont fontBold= wb.createFont();
    fontBold.setBold(true); //set bold
    fontBold.setFontHeight(12); //add font size
    
    rts.append("world ",fontBold);
    rts.append("Hello");
    
    sheet.getRow(1).getCell(1).setCellValue(rts);
    
    0 讨论(0)
  • 2021-01-11 14:34

    This is probably what you are looking for: http://poi.apache.org/spreadsheet/quick-guide.html#DrawingShapes

    Find this in the explanation:

    It's possible to use different fonts to style parts of the text in the textbox. Here's how:

    HSSFFont font = wb.createFont();
    font.setItalic(true);
    font.setUnderline(HSSFFont.U_DOUBLE);
    HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
    string.applyFont(2,5,font);
    textbox.setString(string );
    

    This might be useful: http://apache-poi.1045710.n5.nabble.com/Multiple-text-styles-in-Excel-cell-td4922683.html

    0 讨论(0)
提交回复
热议问题