简体   繁体   中英

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

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

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

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

From here and here for HSSF and 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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM