用Jav a 读取Word 文档 由于Word 的编码方式比较复杂,所以Word 文档不可能通过流的方式直接读取;当然如果Word 可以转化成TXT 文件就可以直接读取了;目前读取Word 比较好的开源工具是Poi 及Jacob,感觉Poi 读取功能要比Jacob 略逊一筹,毕竟Jacob 可以直接调用Word 的COM 组件;但是微软产品不开放源码,所以Jacob 读取Word 文档也只能是摸着石头过河,一点一点破解了。 Jacob 读取Word 内容,由于Word 内容的复杂性,读取也是非常不方便的,目前可以有"按段落读取","按书签读取"及"按照表格读取"等几种形式。 示例讲解(通过Jav a FileReader,Jacob 两种方式读取Word 内容) 一.通过jav a 流读取Word 内容 复制代码 1. import java.io.BufferedReader; 2. import java.io.FileReader; 3. import java.io.IOException; 4. 5. public class ReadWordByStream { 6. public static void main(String[] args) throws IOException { 7. String rowContent = new String(); 8. String content = new String(); 9. BufferedReader in = new BufferedReader(new FileReader("d:\\test3.doc")); 10. while ((rowContent = in.readLine()) != null) { 11. content = content + rowContent + "\n"; 12. } 13. System.out.println(content.getBytes()); 14. System.out.println(new String(content.getBytes(),"utf-8"));//因为编码方式不同,不容易解析 15. in.close(); 16. } 17. 18. } 二.通过Jacob 读取Word 内容 复制代码 1. import com.jacob.activeX.ActiveXComponent; 2. import com.jacob.com.ComThread; 3. import com.jacob.com.Dispatch; 4. import com.jacob.com.Variant; 5. 6. public class WordReader { 7. public static void main(String args[]) { 8. ComThread.InitSTA();// 初始化com 的线程 9. ActiveXComponent wordApp = new ActiveXComponent("Word.Application"); // 启动word 10. // Set the visible property as required. 11. Dispatch.put(wordApp, "Visible", new Variant(true));// //设置word 可见 12. Dispatch docs = wordApp.getProperty("Documents").toDispatch();//所有文档窗口 13. // String inFile = "d:\\test.doc"; 14...