Utopia
Utopia

Reputation: 278

Recover bold text in cell using POI

I'm trying to check if the text in cell (Excel file) is bold or not. For now I'm using :

final CellStyle cellStyle = row.getCell(cellIndex).getCellStyle().toString();

This doesn't give any information about bold text or not. But I've seen that getCellStyle() possesses several methods like "getFontIndex()". Does it exist one to know if the text is bold or not ?

Upvotes: 3

Views: 1522

Answers (1)

jmarkmurphy
jmarkmurphy

Reputation: 11473

This is a bit convoluted, but it is the Font that contains the Bold Attribute, not the CellStyle. You can retrieve it from the CellStyle in a roundabout way though.

Workbook book = row.getSheet().getWorkbook();
CellStyle style = row.getCell(cellIndex).getCellStyle();
int fontIndex = style.getFontIndex();
Font font = book.getFontAt(fontIndex);
if (font.getBold()) {
    // if you are here, the font is bold
}

This will tell you if the CellStyle contains a bold font. The text in the cell can be a RichTextString though, and it can be a totally different font than what is specified in the CellStyle. The RichTextString support is not complete though, so you will have to do some casting to retrieve the font from that if necessary.

Upvotes: 4

Related Questions