现在的位置: 首页 > 综合 > 正文

poi操作word,能控制字体和颜色

2013年10月12日 ⁄ 综合 ⁄ 共 2111字 ⁄ 字号 评论关闭

转帖、摘自:http://topic.csdn.net/u/20081211/15/64547f4c-6fc8-4a20-8131-ba18d7e7b902.html 

 

1 ,new doc :C://blank.doc
2 ,run application

-----------------------

public static void main(String[] args){
try{

FileInputStream in = new FileInputStream("C://blank.doc");
HWPFDocument doc = new HWPFDocument(in);
Range range = doc.getRange();
CharacterProperties props = new CharacterProperties();

// Set the font size in half points
Range currentRange = range;

// Slowly increase the font size
for (int x = 8; x <= 64; x += 4){
// Set the half point size of the font
props.setFontSize(x);
currentRange = currentRange.insertAfter(" Hello World!", props);
}

// Display Bold characters
props.setBold(true);
currentRange = currentRange.insertAfter(" Bold", props);

// Display Italic characters
props.setItalic(true);
currentRange = currentRange.insertAfter(" Italic", props);

// Display charcters with a Double Strikethrough
props.setDoubleStrikeThrough(true);
currentRange = currentRange.insertAfter(" Double Strikethrough",props);

// Insert an empty paragraph for readability
currentRange = currentRange.insertAfter(new ParagraphProperties(),0);

// Reset the character properties
props = new CharacterProperties();
props.setFontSize(32);

// Create a numbered list
HWPFList list = new HWPFList(true, doc.getStyleSheet());
int listID = doc.registerList(list);

// Insert a list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(),listID, 1, 0);
props.setIco24(0xff0000);
currentRange = currentRange.insertAfter(" Blue list entry", props);

// Insert another list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(),listID, 1, 0);
props.setIco24(0xff);
props.setFontSize(38);
props.setCapitalized(true);
currentRange = currentRange.insertAfter(" larger red capitalized",props);

// Last list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(),listID, 1, 0);
props.setIco24(0);
props.setCapitalized(false);
props.setCharacterSpacing(150);
props.setOutline(true);
currentRange = currentRange.insertAfter(" Large character spacing",props);

// Write out the document
FileOutputStream out = new FileOutputStream("C://hello.doc");
doc.write(out);
out.flush();
out.close();

}

catch (Throwable t)

{

t.printStackTrace();

}

}

转帖、摘自:http://topic.csdn.net/u/20081211/15/64547f4c-6fc8-4a20-8131-ba18d7e7b902.html

抱歉!评论已关闭.