开发者

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

开发者 https://www.devze.com 2023-03-25 19:56 出处:网络
How to creat开发者_如何学运维e a RichTextString for Microsoft Excel, using Apache POI framework in Java SE?To keep abstraction on Workbook level (your code will work for both cases - HSSF and XSSF) yo

How to creat开发者_如何学运维e a RichTextString for Microsoft Excel, using Apache POI framework in Java SE?


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

精彩评论

暂无评论...
验证码 换一张
取 消