如何在 Java 中使用 Apache POI 框架创建 RichTextString?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6911934/
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-10-30 17:52:38  来源:igfitidea点击:

How to create a RichTextString using Apache POI framework in Java?

javaexcelexcel-2003

提问by Shumon Saha

How to create a RichTextString for Microsoft Excel, using Apache POI framework in Java SE?

如何在 Java SE 中使用 Apache POI 框架为 Microsoft Excel 创建 RichTextString?

回答by Aram Paronikyan

To keep abstraction on Workbook level (your code will work for both cases - HSSF and XSSF) you can write more intelligent code:

为了保持工作簿级别的抽象(您的代码适用于两种情况 - HSSF 和 XSSF),您可以编写更智能的代码:

Workbook objWorkbook = /*pass workbook as a param*/;
RichTextString richText = objWorkbook.getCreationHelper().createRichTextString("yourstring");

回答by mce

From hereand herefor HSSF and XSSF:

HSSF 和 XSSF从这里这里开始:

HSSFCell hssfCell = row.createCell(idx);
//rich text consists of two runs
HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" );
richString.applyFont( 0, 6, font1 );
richString.applyFont( 6, 13, font2 );
hssfCell.setCellValue( richString );

XSSFRichTextString s1 = new XSSFRichTextString("Apache POI");
s1.applyFont(boldArial);
cell1.setCellValue(s1);

回答by aioobe

From the Quick Guide:

快速指南

Text boxes are created using a different call:

HSSFTextbox textbox1 = patriarch.createTextbox(
        new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)2,2));
textbox1.setString(new HSSFRichTextString("This is a test") );

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 );

使用不同的调用创建文本框:

HSSFTextbox textbox1 = patriarch.createTextbox(
        new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)2,2));
textbox1.setString(new HSSFRichTextString("This is a test") );

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

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 );