永发信息网

求问JAVA怎么读取DOC文件

答案:2  悬赏:0  手机版
解决时间 2021-02-08 10:03
  • 提问者网友:战魂
  • 2021-02-07 10:04
求问JAVA怎么读取DOC文件
最佳答案
  • 五星知识达人网友:玩家
  • 2021-02-07 11:44
用jacob. 其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 jacob下载:

下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子: import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; public class FileExtracter{ public static void main(String[] args) { ActiveXComponent app = new ActiveXComponent("Word.Application"); String inFile = "c:\\test.doc"; String tpFile = "c:\\temp.htm"; String otFile = "c:\\temp.xml"; boolean flag = false; try { app.setProperty("Visible", new Variant(false)); Object docs = app.getProperty("Documents").toDispatch(); Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch(); Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]); Variant f = new Variant(false); Dispatch.call(doc, "Close", f); flag = true; } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); } } } 2。
全部回答
  • 1楼网友:笑迎怀羞
  • 2021-02-07 13:08
word有微软的专用格式,如果要读取其内容,可以使用jar包,如下: 1。用jacob. 其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 jacob下载: http://www.matrix.org.cn/down_view.asp?id=13 下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子: import java.io.file; import com.jacob.com.*; import com.jacob.activex.*; public class fileextracter{ public static void main(string[] args) { activexcomponent app = new activexcomponent("word.application"); string infile = "c:\\test.doc"; string tpfile = "c:\\temp.htm"; string otfile = "c:\\temp.xml"; boolean flag = false; try { app.setproperty("visible", new variant(false)); object docs = app.getproperty("documents").todispatch(); object doc = dispatch.invoke(docs,"open", dispatch.method, new object[]{infile,new variant(false), new variant(true)}, new int[1]).todispatch(); dispatch.invoke(doc,"saveas", dispatch.method, new object[]{tpfile,new variant(8)}, new int[1]); variant f = new variant(false); dispatch.call(doc, "close", f); flag = true; } catch (exception e) { e.printstacktrace(); } finally { app.invoke("quit", new variant[] {}); } } } 2。用apache的poi来抽取word,excel。 poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: 下载经过封装后的poi包: http://www.matrix.org.cn/down_view.asp?id=14 下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: import java.io.*; import org.textmining.text.extraction.wordextractor; public class pdfextractor { public pdfextractor() { } public static void main(string args[]) throws exception { fileinputstream in = new fileinputstream ("c:\\a.doc"); wordextractor extractor = new wordextractor(); string str = extractor.extracttext(in); system.out.println("the result length is"+str.length()); system.out.println("the result is"+str); } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯