JAVA实现Doc与Docx互转

一、开发须知

Jacob是Java-COMBridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。

COMcomponent(COM组件)是微软公司为了计算机工业的软件生产更加符合人类的行为方式开发的一种新的软件开发技术。在COM构架下,人们可以开发出各种各样的功能专一的组件,然后将它们按照需要组合起来,构成复杂的应用系统。由此带来的好处是多方面的:可以将系统中的组件用新的替换掉,以便随时进行系统的升级和定制;可以在多个应用系统中重复利用同一个组件;可以方便的将应用系统扩展到网络环境下;COM与语言,平台无关的特性使所有的程序员均可充分发挥自己的才智与专长编写组件模块。

ActiveX component:一个已经编译好的基于COM的软件组件,它封装了一组商务功能。ActiveX组件中的功能是通过ActiveX自动化接口来访问的。ActiveX组件既可在客户机上执行,也可在服务器上执行,且通过DCOM实现了对调用程序的透明化。


二、开发Jar包

http://链接: https://pan.baidu.com/s/1sluMdzB 密码: 73bg(链接失效,自行解决)

复制jacob.jar包到项目lib到下,对应本地操作系统复制jacob-1.18-x64.dll/jacob-1.18-x86.dll到系统盘:/Windows/System32下
三、开发代码

import com.jacob.activeX.ActiveXComponent; 
import com.jacob.com.ComThread; 
import com.jacob.com.Dispatch; 
import com.jacob.com.Variant; 
import java.io.File; 
// 格式大全:前缀对应以下方法的fmt值 
// 0:Microsoft Word 97 - 2003 文档 (.doc) 
// 1:Microsoft Word 97 - 2003 模板 (.dot) 
// 2:文本文档 (.txt) 
// 3:文本文档 (.txt) 
// 4:文本文档 (.txt) 
// 5:文本文档 (.txt) 
// 6:RTF 格式 (.rtf) 
// 7:文本文档 (.txt) 
// 8:HTML 文档 (.htm)(带文件夹) 
// 9:MHTML 文档 (.mht)(单文件) 
// 10:MHTML 文档 (.mht)(单文件) 
// 11:XML 文档 (.xml) 
// 12:Microsoft Word 文档 (.docx) 
// 13:Microsoft Word 启用宏的文档 (.docm) 
// 14:Microsoft Word 模板 (.dotx) 
// 15:Microsoft Word 启用宏的模板 (.dotm) 
// 16:Microsoft Word 文档 (.docx) 
// 17:PDF 文件 (.pdf) 
// 18:XPS 文档 (.xps) 
// 19:XML 文档 (.xml) 
// 20:XML 文档 (.xml) 
// 21:XML 文档 (.xml) 
// 22:XML 文档 (.xml) 
// 23:OpenDocument 文本 (.odt) 
// 24:WTF 文件 (.wtf) 
/** 
* 使用jacob进行Word文档格式互转(例:doc2docx、docx2doc) 
* 
* @author Harley Hong 
* @created 2017 /08/09 16:09:32 
*/ 
public class DocFmtConvert { 
/** 
* doc格式 
*/ 
private static final int DOC_FMT = 0; 
/** 
* docx格式 
*/ 
private static final int DOCX_FMT = 12; 
/** 
* 描述 The entry point of application. 
* 
* @param args 
* the input arguments 
* @author Harley Hong 
* @created 2017 /08/09 16:14:44 
*/ 
public static void main(String[] args) { 
DocFmtConvert dfc = new DocFmtConvert(); 
String srcDocPath = "E://test.docx"; 
String descDocPath = "E://test.doc"; 
try { 
dfc.convertDocFmt(srcDocPath, descDocPath, DOC_FMT); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
/** 
* 根据格式类型转换doc文件 
* 
* @param srcPaththe doc path 源文件 
* @param descPath the docx path 目标文件 
* @param fmtthe fmt 所转格式 
* @return the file 
* @throws Exception the exception 
* @author Harley Hong 
* @created 2017 /08/09 16:14:07 Convert docx 2 doc file. 
*/ 
public File convertDocFmt(String srcPath, String descPath, int fmt) throws Exception {
 // 实例化ComThread线程与ActiveXComponent 
ComThread.InitSTA(); 
ActiveXComponent app = new ActiveXComponent("Word.Application"); 
try { 
// 文档隐藏时进行应用操作 
app.setProperty("Visible", new Variant(false)); 
// 实例化模板Document对象 
Dispatch document = app.getProperty("Documents").toDispatch(); 
// 打开Document进行另存为操作 
Dispatch doc = Dispatch.invoke(document, "Open", Dispatch.Method, 
new Object[] { srcPath, new Variant(true), new Variant(true) }, new int[1]).toDispatch();
 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { descPath, new Variant(fmt) }, new int[1]);
 Dispatch.call(doc, "Close", new Variant(false)); 
return new File(descPath); 
} catch (Exception e) { 
throw e; 
} finally { 
// 释放线程与ActiveXComponent 
app.invoke("Quit", new Variant[] {}); 
ComThread.Release(); 
} 
} 
} 

转载自阿里云社区 https://www.aliyun.com/jiaocheng/783516.html

你可能感兴趣的:(JAVA实现Doc与Docx互转)