java jacob 简易输出内容到Word中。。。

1,首先需要下载jacob包http://sourceforge.net/projects/jacob-project/ ,并将jar放入lib包下,将对应系统的dll放入System32文件夹下,如果配置后的运行显示错误,可尝试下载jacob 1.11版本的包

2,java代码片段

 1 ActiveXComponent MsWordApp = new ActiveXComponent("Word.Application"); 
2 Dispatch.put(MsWordApp, "Visible",new Variant(true));
3 Dispatch documents =Dispatch.get(MsWordApp,"Documents").toDispatch();
4 Dispatch document = Dispatch.call(documents,"Add").toDispatch();
5
6 Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch();
7 Dispatch.put(selection,"Text","要插入的内容");
8 String path = request.getSession().getServletContext().getRealPath("\\")+"words\\";
9 File f=new File(path);
10 if(!f.exists()){
11 f.mkdirs();
12 }
13 Dispatch.call(document,"SaveAs",path+System.currentTimeMillis());
14 Dispatch.call(document, "Close", new Variant(0));
15 Dispatch.call(MsWordApp,"Quit");
16 MsWordApp = null;
17 document = null;



posted @ 2012-03-26 11:42  将来的老大爷  阅读(447)  评论(0编辑  收藏  举报

如果本页面列出的内容侵犯了您的权益,请告知。
知识共享许可协议
996.icu