日期:2014-05-20  浏览次数:20688 次

java读取word文档,高手指教!
大家好,我想用java实现对word文档内容的读取,不知道该用什么方法,高手帮我一下吧,谢啦。

------解决方案--------------------
网上找来的用java读取word文档的代码
public class WordTest {

public static void main(String[] args){

FileOutputStream ostream=null;
FileInputStream istream=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();
}
}
}
}


------解决方案--------------------
import java.io.*;
import org.apache.poi.hwpf.extractor.WordExtractor;