2019-07-09_Java通过JACOB操作WPS记录
Java通过JACOB操作WPS记录
1概述
JACOB-JavaCOMBridge标准的操作word、excel工具包。用户本地系统中安装Word的应用程序可选(office、wps有其一即可)。
1.****1****下载****JACOB
image.png1.2环境准备
本机是64位,拷贝机器版本部署对应的jacob-1.19-x64.dll到C:\Program Files\Java\jdk1.8.0_60\bin。
将jacob.jar放到响应的工程目录即可。
2 wps实战
2.1.Word2PDF
2.1.1 Word2Pdf
package com.tech.ability.myjacob;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;
/**
-
@author kikop
-
@version 1.0
-
@project Name: TechnicalAbilityToolBox
-
@file Name: MyWord2PDF
-
@desc 功能描述
-
@date 2019/7/8
-
@time 23:01
-
@by IDE: IntelliJ IDEA
*/
public class MyWord2PDF {
private static final int wdFormatPDF = 17; // PDF 格式
public void wordToPDF(String sfileName, String toFileName) {
System.out.println("启动 Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try {
// app = new ActiveXComponent("Word.Application"); // 基于microsoft office
app = new ActiveXComponent("KWPS.Application"); // 基于kwps的方式
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
System.out.println("打开文档..." + sfileName);
System.out.println("转换文档到 PDF..." + toFileName);
File tofile = new File(toFileName);
if (tofile.exists()) { // 目标pdf存在,则删除,前提未使用
tofile.delete();
}
Dispatch.call(doc, "SaveAs", toFileName, // FileName
wdFormatPDF);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(doc, "Close", false);
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[]{});
}
// 如果没有这句话,winword.exe进程将不会关闭
ComThread.Release();
}
}
2.1.2 测试
package com.tech.ability.myjacob;
/**
-
@author kikop
-
@version 1.0
-
@project Name: TechnicalAbilityToolBox
-
@file Name: MyWord2PDFTest
-
@desc 功能描述
-
@date 2019/7/8
-
@time 23:04
-
@by IDE: IntelliJ IDEA
*/
public class MyWord2PDFTest {
/**
*
* @param args
*/
public static void main(String[] args) {
MyWord2PDF d = new MyWord2PDF();
d.wordToPDF("D:/Java通过jacob操作wps记录.doc", "D:/Java通过jacob操作wps记录.pdf");
}
}
2.1.3 结果查看
image.png2.2 合并多个Word为一个Word
2.2.1 mergeMultipleWord2Single
/**
* 合并多个word为一个Word
*
* @param srcdocs
* @param destDoc
* @return
*/
public boolean mergeMultipleWord2Single(java.util.List srcdocs, String destDoc) {
//1.校验
if (srcdocs.size() == 0 || srcdocs == null) {
return false;
}
System.out.println("启动 Word...");
long start = System.currentTimeMillis();
//2.判断
ActiveXComponent app = null;
Object doc = null;
try {
// app = new ActiveXComponent("Word.Application"); // 基于microsoft office
app = new ActiveXComponent("KWPS.Application"); // 基于kwps的方式,具体和调用ActiveX控件有关
// 2.1.设置word不可见
app.setProperty("Visible", new Variant(false));
//获得Documents对象
Object docs = app.getProperty("Documents").toDispatch();
// 2.2.打开第一个文件
doc = Dispatch.invoke(
(Dispatch) docs, // 参数1:调用目标
"Open", // 参数2
Dispatch.Method, //参数3
new Object[]{(String) srcdocs.get(0), new Variant(false), new Variant(true)}, // 参数4
new int[3] //参数5
).toDispatch();
// 2.3.追加后续文件
for (int i = 1; i < srcdocs.size(); i++) {
Dispatch.invoke(
app.getProperty("Selection").toDispatch(), //参数1
"insertFile",
Dispatch.Method,
new Object[]{(String) srcdocs.get(i), "", new Variant(false), new Variant(false), new Variant(false)}, // 参数4
new int[3] // 参数5
);
}
//2.4.如果目的word存在,则删除
File tofile = new File(destDoc);
if (tofile.exists()) { // 目标pdf存在,则删除,前提未使用
tofile.delete();
}
// 2.5.保存为新的word
Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method, new Object[]{destDoc, new Variant(1)}, new int[3]);
Variant f = new Variant(false);
Dispatch.call((Dispatch) doc, "Close", f); // 把close属性赋值个f
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========合并Word文件失败:" + e.getMessage());
//throw new RuntimeException("========合并Word文件失败:" + e);
return false;
} finally {
System.out.println("关闭文档");
if (app != null) {
app.invoke("Quit", new Variant[]{});
}
}
// 如果没有这句话,winword.exe进程将不会关闭
ComThread.Release();
return true;
}
2.2.2 测试
/**
* word转pdf
*/
public static void mergeMultipleWord2SingleTest() {
MyWord2PDF d = new MyWord2PDF();
String descDoc = "D:/Java通过jacob操作wps记录_new.doc";
String descPdf = "D:/Java通过jacob操作wps记录_new.pdf";
List srcDocs = new ArrayList<>();
srcDocs.add("D:/Java通过jacob操作wps记录1.doc");
srcDocs.add("D:/Java通过jacob操作wps记录2.doc");
boolean mergeResult = d.mergeMultipleWord2Single(srcDocs, descDoc);
if (mergeResult) {
System.out.println("合并成功!");
d.wordToPDF(descDoc, descPdf);
} else {
System.out.println("合并失败!");
}
}
2.2.3 结果查看
image.png2.3合并后的Word生成PDF
参见2.1.1
3常见错误及解决
3.1.错误一
错误:
文档转换失败:Can't get object clsid from progid
解决:
主要是创建ActiveXComponent的姿势不对,调整如下:
//app = new ActiveXComponent("Word.Application"); // 基于microsoft office app = new ActiveXComponent("KWPS.Application"); // 基于kwps的方式