Java 使用 apache poi 将单元格内容的一部分设置为粗体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9697330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:17:51  来源:igfitidea点击:

Setting a part of the cell contents to bold using apache poi?

javahtmlapache-poi

提问by Saicharan S M

How can i put this string in to a cell using java apache poi?
The string is "Hello worldHello"
As u can see i need to make a part of the text bold?
I'm able to set the entire contents of the cell to bold but not specific parts.
Please help me.

如何使用 java apache poi 将此字符串放入单元格中?
字符串是“Hello worldHello”
如您所见,我需要将文本的一部分设为粗体?
我可以将单元格的全部内容设置为粗体,但不能将特定部分设置为粗体。
请帮我。

采纳答案by Michael Fernando

This is probably what you are looking for: http://poi.apache.org/spreadsheet/quick-guide.html#DrawingShapes

这可能就是你要找的:http: //poi.apache.org/spreadsheet/quick-guide.html#DrawingShapes

Find this in the explanation:

在解释中找到这个:

It's possible to use different fonts to style parts of the text in the textbox. Here's how:

可以使用不同的字体来设置文本框中部分文本的样式。就是这样:

HSSFFont font = wb.createFont();
font.setItalic(true);
font.setUnderline(HSSFFont.U_DOUBLE);
HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
string.applyFont(2,5,font);
textbox.setString(string );

This might be useful: http://apache-poi.1045710.n5.nabble.com/Multiple-text-styles-in-Excel-cell-td4922683.html

这可能有用:http: //apache-poi.1045710.n5.nabble.com/Multiple-text-styles-in-Excel-cell-td4922683.html

回答by Arif Nazar Purwandaru

This will print "Hello worldHello" in a cell

这将在单元格中打印“Hello worldHello”

XSSFRichTextString rts= new XSSFRichTextString("Hello ");

XSSFFont fontBold= wb.createFont();
fontBold.setBold(true); //set bold
fontBold.setFontHeight(12); //add font size

rts.append("world ",fontBold);
rts.append("Hello");

sheet.getRow(1).getCell(1).setCellValue(rts);