2016-05-26 38 views
3

我試圖檢查單元格(Excel文件)中的文本是否爲粗體。 現在我使用:使用POI恢復單元格中的粗體文本

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

這沒有給出任何關於粗體文字或沒有任何信息。但是我已經看到getCellStyle()擁有幾個方法,比如「getFontIndex()」。它是否存在一個知道文本是否大膽?

回答

3

這有點複雜,但它是Font包含粗體屬性,而不是CellStyle。儘管如此,你可以通過迂迴的方式從CellStyle中檢索它。

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 
} 

這會告訴你CellStyle是否包含粗體字體。雖然單元格中的文本可能是RichTextString,並且它可能與CellStyle中指定的字體完全不同。雖然RichTextString支持並不完整,所以如果需要,您必須執行一些轉換才能從中檢索字體。

+0

謝謝,它很好。 – Utopia

相關問題