45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:如何用java读取word文档代码?

如何用java读取word文档代码?

2016-09-08 18:42:36 来源:www.45fan.com 【

如何用java读取word文档代码?

public class WordTest {

public static void main(String[] args){

FileOutputStream ostream=null;
FileInputStreamistream=null;
try{
istream=new FileInputStream("c://test.doc");

HWPFDocument doc=new HWPFDocument(istream);
ostream =new FileOutputStream("C://testout.doc");


CharacterProperties props = new CharacterProperties();

Range range=doc.getRange();
// Slowly increase the font size
for (int x = 8; x <= 64; x += 4){
// Set the half point size of the font
props.setFontSize(x);
range.insertAfter("Test write information into file",props);
range.insertAfter("/n/r");
}

// Display Bold characters
props.setBold(true);
range.insertAfter(" Bold", props);
range.insertAfter("/n/r");

// Display Italic characters
props.setItalic(true);
range.insertAfter(" Italic", props);
range.insertAfter("/n/r");

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

// Insert an empty paragraph for readability
range.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

range.insertAfter(new ParagraphProperties(), listID, 1,0);
props.setIco24(0xff0000);
range.insertAfter(" Blue list entry", props);

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

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

doc.write(ostream);
ostream.flush();
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
try{
if(ostream!=null){
ostream.close();
}
if(istream!=null){
istream.close();
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
}
 

本文地址:http://www.45fan.com/dnjc/73758.html
Tags: 网上 Java 找来
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部