为什么要用POI。因为POI是一款简单的,用于抽取word文档成string的开源java。
一,下载POI
首先从http://poi.apache.org/下载二进制发布包。
poi-bin-3.8-beta4-20110826.zip
二,解压
三,在eclipse中新建一个java项目 导入日志相关的包commons-logging-1.1.jar、log4j-1.2.13.jar。
还有poi-3.8-beta4-20110826.jar以及poi-scratchpad-3.8-beta4-20110826.jar。
导入的过程:一是建立lib目录;二是向目录中拷入上述四个文件;三是,用build path ——add jars,导入相关包文件。
四,第一个POI程序
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class TestWord {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
String filesname = "Hello.doc";
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filesname));
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
String str = we.getText() ;
System.out.println(str);
}
}