How to apply bold text style for a range of text i

2019-08-13 02:09发布

问题:

How to make a range of text bold text style using Apache POI? Eg:

Instead of applying style for the entire cell. I used to do this in vb.net with these lines of code:

excellSheet.Range("C2").Value = "Priority: " + priority
excellSheet.Range("C2").Characters(0, 8).Font.Bold = True

But I can't find the way to do that in Java using Apache POI.

Any help will be much appreciated. Thanks!

回答1:

First, create your Font with bold styling, using the Workbook object.

Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);

Next, grab the RichTextString from the Cell and call the applyFont overload that takes a range of indexes and the Font to apply.

RichTextString rts = cell.getRichStringCellValue();
rts.applyFont(0, 8, font);

You should reuse the Font object if you want to convert other text in the workbook to bold.