现在Jacob维护者将项目托管到了GitHub
Jacob下载:
https://github.com/freemansoft/jacob-project
我调用的是WPS Office,使用MicroSoft Office请根据实际情况修改
office其他操作请参阅:
https://docs.microsoft.com/zh-cn/office/vba/api/overview/
wps其他操作请参阅:
https://open.wps.cn/docs/office
2020/10/28补充
我个人的电脑在设置系统默认办公套
件为WPS后
axc = new ActiveXComponent("Word.Application") 设置无效
Word转PDF:
* word转PDF
*
@param
inFilePath 输入文件全路径
*
@param
outFilePath 输出文件全路径
public
static
void
wordToPdf(String inFilePath, String outFilePath) {
System.out.println(
"inFilePath = " +
inFilePath);
System.out.println(
"outFilePath = " +
outFilePath);
Long start
=
System.currentTimeMillis();
//
构建ActiveX组件实例
ActiveXComponent axc =
null
;
//
初始化Dispatch对象
Dispatch word =
null
;
try
{
//
初始化com线程(不初始化会出现异常)
ComThread.InitSTA();
//操作Microsoft Office
axc = new ActiveXComponent("Word.Application");
//
操作WPS Office
axc =
new
ActiveXComponent("KWPS.Application"
);
//
设置应用操作不开启窗口,在后台静默处理
axc.setProperty("Visible",
new
Variant(
false
));
//
获得该ActiveX控件的控制权
word = axc.getProperty("Documents"
).toDispatch();
//
方法参数包装(具体的去看巨硬或者WPS的VBA文档)
Object[] params1 =
{
inFilePath,
//
文档名(可包含路径。我这里是方法直接接收的全路径)
new
Variant(
false
),
//
如果该属性为 True,则当文件不是word格式时,将显示“文件转换”对话框
new
Variant(
true
)
//
如果该属性值为 True,则以只读方式打开文档。该参数不会覆盖保存的文档的只读建议设置。例如,如果文档在只读建议启用的情况下保存,则将 ReadOnly 参数设置为 False 不会导致文件以可读写方式打开
word
= Dispatch.invoke(word, "Open", Dispatch.Method, params1,
new
int
[1
]).toDispatch();
//
创建一个文件对象
File file =
new
File(outFilePath);
//
检查磁盘中是否已经存在该文件,若存在就删除
if
(file.exists()) {
boolean
delete =
file.delete();
Object[] params2
=
{
outFilePath,
//
新的 PDF 或 XPS 文件的路径和文件名
new
Variant(17)
//
指定采用 PDF:17 格式或 XPS:18 格式
Dispatch.invoke(word,
"ExportAsFixedFormat", Dispatch.Method, params2,
new
int
[1
]);
Long end
=
System.currentTimeMillis();
System.out.println(
"文档转换成功·用时:" + (end - start) + "ms."
);
}
catch
(Exception e) {
e.printStackTrace();
System.out.println(
"文档转换失败:" +
e.getMessage());
}
finally
{
//
关闭Word
if
(word !=
null
) {
Dispatch.call(word,
"Close",
false
);
if
(axc !=
null
) {
axc.invoke(
"Quit",
new
Variant[]{});
//
关闭com线程(不关闭会损耗系统性能)
ComThread.Release();
Excel转PDF(可以将所有sheet全部输出为一个PDF):
* excel转PDF
*
@param
inFilePath 输入文件全路径
*
@param
outFilePath 输出文件全路径
public
static
void
excelToPdf(String inFilePath, String outFilePath) {
System.out.println(
"inFilePath = " +
inFilePath);
System.out.println(
"outFilePath = " +
outFilePath);
Long start
=
System.currentTimeMillis();
ActiveXComponent axc
=
null
;
Dispatch excel
=
null
;
try
{
ComThread.InitSTA();
//
axc = new ActiveXComponent("Excel.Application");
axc =
new
ActiveXComponent("KET.Application"
);
axc.setProperty(
"Visible",
new
Variant(
false
));
//
禁用宏
axc.setProperty("AutomationSecurity",
new
Variant(3
));
//
打开Excel文件
excel = axc.getProperty("Workbooks"
).toDispatch();
Object[] params1
=
new
Object[]{
inFilePath,
new
Variant(
false
),
new
Variant(
false
)
//
如果为 True,则以只读模式打开工作簿
excel
= Dispatch.invoke(excel, "Open", Dispatch.Method, params1,
new
int
[9
])
.toDispatch();
//
创建一个文件对象
File file =
new
File(outFilePath);
//
检查磁盘中是否已经存在该文件,若存在就删除
if
(file.exists()) {
boolean
delete =
file.delete();
//
转换格式
Object[] params2 =
new
Object[]{
new
Variant(0),
//
指定采用 PDF:0 格式或 XPS:1 格式
outFilePath,
new
Variant(0)
//
指定采用 标准:0(图片不会变模糊) 质量 或者 压缩:1(可以减小文件体积) 质量
Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, params2,
new
int
[1
]);
Long end
=
System.currentTimeMillis();
System.out.println(
"文档转换成功·用时:" + (end - start) + "ms."
);
}
catch
(Exception e) {
e.printStackTrace();
System.out.println(
"文档转换失败:" +
e.getMessage());
}
finally
{
//
关闭Excel
if
(excel !=
null
) {
Dispatch.call(excel,
"CLose",
new
Variant(
false
));
if
(axc !=
null
) {
axc.invoke(
"Quit",
new
Variant[]{});
ComThread.Release();
Dispatch.invoke()
* Dispatch.invoke方法参数
*
@param
dispatchTarget Dispatch对象
*
@param
name 方法名
*
@param
wFlags 调度方法
*
@param
oArg 方法参数数组
*
@param
uArgErr 错误参数码
*
@return
Dispatch对象