How to apply bold text style for a range of text inside a cell using Apache POI?

谁说胖子不能爱 提交于 2019-12-11 06:58:13

问题


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.



来源:https://stackoverflow.com/questions/30851788/how-to-apply-bold-text-style-for-a-range-of-text-inside-a-cell-using-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!