简介:
jacob(java com bridge,java com桥)分为两个部分,jacob.jar,jacob.dll,使用时两个东西的版本要一致,而且还分32位和64位,它的位数和jdk的位数有关,与操作系统的位数无关。它的原理是通过java的jni功能,调用系统组件dll,通过这个com桥来操作com组件,最终完成对office文档的操作。
环境配置:
1、在工程中引入jar包
2、将dll文件放到系统的path变量中,或者直接放到jdk/bin、jre/bin、system32/system64下面都放一份,总能找得到,版本要急着对应好。
常用类以及方法
ComThread:com组件管理,用来初始化com线程,释放线程,所以会在操作office之前使用,操作完成再使用。
ActiveXComponent:创建office的一个应用,比如你操作的是word还是excel
Dispatch:调度处理类,封装了一些操作来操作office,里面所有的可操作对象基本都是这种类型,所以jacob是一种链式操作模式,就像StringBuilder对象,调用append()方法之后返回的还是StringBuilder对象
Variant:封装参数数据类型,因为操作office是的一些方法参数,可能是字符串类型,可能是数字类型,虽然都是1,但是不能通过,可以通过Variant来进行转换通用的参数类型,new Variant(1),new Variant("1")
Dispatch的几种静态方法:这些方法就是要用来操作office的。
•call( )方法:调用COM对象的方法,返回Variant类型值。
•invoke( )方法:和call方法作用相同,但是不返回值。
•get( )方法:获取COM对象属性,返回variant类型值。
•put( )方法:设置COM对象属性。
例1、Word转pdf
/**
* word文件格式转换
*
* @param sourceFilePath(输入路径)
* @param destFilePath(输出路径)
* @param format (转pdf参数为17)
*/
private static void convert(String sourceFilePath, String destFilePath, int format) {
// 初始化com的线程
ComThread.InitSTA();
ActiveXComponent app = null;
Dispatch doc = null;
try {
logger.debug("启动word...");
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
// 打开word文档
logger.debug("打开文档...:" + sourceFilePath);
doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { sourceFilePath, new Variant(false), new Variant(true)/* ReadOnly */, new Variant(false),
new Variant("")/* password */, new Variant(""), new Variant(true) /* Revert */, new Variant(""),
new Variant(""), new Variant(0)/* Format */, new Variant(0)/* Encoding */, new Variant(false)/* Visible */,
new Variant(false)/* OpenAndRepair */, new Variant(0), new Variant(true) /* NoEncodingDialog */},
new int[1]).toDispatch();
logger.debug("转换...:" + destFilePath);
File tofile = new File(destFilePath);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, "SaveAs", destFilePath, format);
} catch (Throwable e) {
// //异常时调用,很关键
// ComThread.doCoUninitialize();
throw new RuntimeException("转换失败", e);
} finally {
if (doc != null) {
try {
logger.debug("关闭文档...");
Dispatch.call(doc, "Close", new Variant(false));
} catch (Throwable e) {
logger.error("关闭文档失败", e);
}
}
try {
try {
if (app != null) {
logger.debug("退出word...");
// app.invoke("Quit", new Variant[] {});
app.invoke("Quit", new Variant(0), new Variant(0), new Variant(false));
}
} catch (Throwable e) {
logger.error("退出word失败", e);
}
// 如果没有这句话,winword.exe进程将不会关闭
// 关闭com的线程
ComThread.Release();
logger.debug("转换完毕");
} catch (Exception e) {
logger.error("退出word失败", e);
}
}
}
例2、判断是否装了office,及获得office版本号:
ActiveXComponent app = null;
try{
app = new ActiveXComponent("Word.Application");// 启动word
String version = "0";
if(Util.notNull(app)){
version = app.getProperty("version").toString();
}
System.out.println(version);
}catch(Exception e){
//e.printStackTrace();
}finally{
try {
if (app != null) {
// app.invoke("Quit", new Variant[] {});
app.invoke("Quit", new Variant(0), new Variant(0), new Variant(false));
}
} catch (Throwable e) {
// logger.error("退出word失败", e);
}
}
Office版本号:
office2013 版本号——15
office2010 版本号——14
office2007 版本号——12
office2003 版本号——11