如何用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