如何使用 Apache POI 为单元格内的一系列文本应用粗体文本样式? [英] How to apply bold text style for a range of text inside a cell using Apache POI?
问题描述
如何使用 Apache POI 制作一系列文本粗体文本样式?例如:
How to make a range of text bold text style using Apache POI? Eg:
而不是为整个单元格应用样式.我曾经在 vb.net 中使用以下代码行执行此操作:
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
但我找不到在 Java 中使用 Apache POI 做到这一点的方法.
But I can't find the way to do that in Java using Apache POI.
任何帮助将不胜感激.谢谢!
Any help will be much appreciated. Thanks!
推荐答案
首先,使用 Workbook
对象,以粗体样式创建您的 Font
.
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
接下来,从 Cell
中获取 RichTextString
并调用 applyFont
重载,需要一个范围索引和 Font
应用.
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);
如果要将工作簿中的其他文本转换为粗体,则应重用 Font
对象.
You should reuse the Font
object if you want to convert other text in the workbook to bold.
这篇关于如何使用 Apache POI 为单元格内的一系列文本应用粗体文本样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!