JACOB 简介
Jacob 是 JAVA-COM Bridge的缩写,是一个中间件,能够提供自动化访问MS系统下COM组件和Win32 libraries的功能。
• MS系统提供的COM组件
COM组件 对象ID
MS Word Word.Application
MS Excel Excel.Application
MS Powerpoint Powerpoint.Application
MS IE InternetExplore.Application
使用方法
一、下载(下载方式搜索引擎搜索,我的资源包里也有上传)
二、按电脑系统将下载好的jacob-1.19-x64.dll 或 jacob-1.19-x86.dll 放到 java安装路径bin目录下
二、将jar安装到maven本地仓库
参考:mvn install:install-file -Dfile=C:\Users\Minco\Desktop\jacob.jar -DgroupId=com.hieasy -DartifactId=jacob -Dversion=1.0 -Dpackaging=jar
三、pom引入依赖
<dependency>
<groupId>com.hieasy</groupId>
<artifactId>jacob</artifactId>
<version>1.0</version>
</dependency>
四、调用
@RequestMapping(value = "/print")
public @ResponseBody void print(HttpServletResponse response){
String path="C:\\Users\\Minco\\Desktop\\PF1000001.docx";
printDoc(path);
}
void printDoc(String path){
System.out.println("开始打印");
ActiveXComponent word=new ActiveXComponent("Word.Application");
Dispatch doc=null;
Dispatch.put(word, "Visible", new Variant(false));
Dispatch docs=word.getProperty("Documents").toDispatch();
doc=Dispatch.call(docs, "Open", path).toDispatch();
try {
Dispatch.call(doc, "PrintOut");//打印
} catch (Exception e) {
e.printStackTrace();
System.out.println("打印失败");
}finally{
try {
if(doc!=null){
Dispatch.call(doc, "Close",new Variant(0));
}
} catch (Exception e2) {
e2.printStackTrace();
}
//释放资源
ComThread.Release();
}
}
成果展示
延展问题
word模板如何制作成实际的word文档,目前笔者方式是word模板另存为xml格式,然后修改为freemarker ftl后缀,然后利用freemarker生成word文档。有兴趣的可以留言讨论。