Apache POI: Partial Cell Fonts

If I hack into MS Excel (suppose), or LibreOffice Calc (checked), I can enter the material in the cell and change the font of the parts of the text in the cell, for example, make it in one cell,

This text is in bold and this text is in italics.

Again, let me reiterate that this row can exist in the format shown in one cell.

Is it possible to achieve this level of customization using Apache POI? The search only shows how to apply the font to the entire cell.

thank

=== UPDATE ===

As suggested below, I ended up working with HSSFRichTextString (since I work with HSSF). However, after applying fonts (I tried to make bold and underlined), my text would remain unchanged. This is what I tried. To put things in context, I'm working on something sporty, which usually displays a match in the form of "awayteam" @ "hometeam", and depending on certain external conditions, I would like to make one or the other - bold. My code looks something like this:

    String away = "foo";
    String home = "bar";
    String bolden = "foo"
    HSSFRichTextString val = new HSSFRichTextString(away+"@"+home);

    if(bolden.equals(home)) {
        val.applyFont(val.getString().indexOf("@") + 1, val.length(), Font.U_SINGLE);
    } else if(bolden.equals(away)) {
        val.applyFont(0, val.getString().indexOf("@"), Font.U_SINGLE);
    }
    gameHeaderRow.createCell(g + 1).setCellValue(val);

As you can see, this is a piece of code from a more complex function than it is displayed, but in fact it is the main code. As you can see, I am doing val.applyFont for part of the row and then setting the value of the cell with the row. So I’m not quite sure what I did wrong. Any advice is appreciated.

thank

Kfj

+5
3

POI , , , RichTextString. , RichTextString , , -.

+5
val.applyFont(0, val.getString().indexOf("@"), Font.U_SINGLE);

You should not pass Font.U_SINGLEin applyFont, but a new font, for example new HSSFFont(), then setUnderline(Font.U_SINGLE).

Example:

HSSFFont f1 = new HSSFFont();
f1.setUnderline(Font.U_SINGLE);
val.applyFont(0, val.getString().indexOf("@"), f1);
0
source

All Articles