电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

用Java读取Word文档

用Java读取Word文档_第1页
1/7
用Java读取Word文档_第2页
2/7
用Java读取Word文档_第3页
3/7
用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...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

用Java读取Word文档

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部