package com.github.hyarp.core.jacob;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public enum MSOfficeApplication {
Word("Word.Application") {
@Override
public Dispatch openDocument(ActiveXComponent activeXComponent, Object... attributes) {
Dispatch docs = Dispatch.get(activeXComponent, "Documents").getDispatch();
return Dispatch.call(docs, "Open", attributes).getDispatch();
}
@Override
public void asPdf(String officeFile, String pdfFile) {
convert(officeFile, "ExportAsFixedFormat", pdfFile, 17);
}
@Override
public void asHtml(String officeFile, String htmlFile) {
convert(officeFile, "SaveAs", htmlFile, 8);
}
},
Excel("Excel.Application") {
@Override
public Dispatch openDocument(ActiveXComponent activeXComponent, Object... attributes) {
Dispatch docs = Dispatch.get(activeXComponent, "Workbooks").getDispatch();
return Dispatch.call(docs, "Open", attributes).getDispatch();
}
@Override
public void asPdf(String officeFile, String pdfFile) {
convert(officeFile, "ExportAsFixedFormat", 0, pdfFile);
}
@Override
public void asHtml(String officeFile, String htmlFile) {
convert(officeFile, "SaveAs", htmlFile, 44);
}
},
PowerPoint("PowerPoint.Application") {
@Override
public Dispatch openDocument(ActiveXComponent activeXComponent, Object... attributes) {
Dispatch ppts = activeXComponent.getProperty("Presentations").getDispatch();
return Dispatch.call(ppts, "Open", attributes).getDispatch();
}
@Override
public void asPdf(String officeFile, String pdfFile) {
convert(officeFile, "ExportAsFixedFormat", pdfFile, 32);
}
@Override
public void asHtml(String officeFile, String htmlFile) {
convert(officeFile, "SaveAs", htmlFile, 12);
}
};
/**
* 打开文档
*
* @param activeXComponent office activeX组件
* @param attributes 参数
* @return
*/
public abstract Dispatch openDocument(ActiveXComponent activeXComponent, Object... attributes);
/**
* office文档转换成pdf文档
*
* @param officeFile office文档路径,此路径必须是windows下绝对路径
* @param pdfFile 保存的pdf文件路径,此路径必须是windows下绝对路径
*/
public abstract void asPdf(String officeFile, String pdfFile);
/**
* office文档转换成html文档
*
* @param officeFile office文档路径,此路径必须是windows下绝对路径
* @param htmlFile 保存的html文档路径,此路径必须是windows下绝对路径
*/
public abstract void asHtml(String officeFile, String htmlFile);
/**
* 转换文档
*
* @param source 源文档路径,此路径必须是windows下绝对路径
* @param attributes 参数
*/
public void convert(String source, String cmd, Object... attributes) {
ActiveXComponent activeXComponent = null;
Dispatch doc = null;
try {
//初始化应用组件
activeXComponent = initActiveXComponent();
//打开文档,第三个参数代表是否采用只读模式打开文档
doc = openDocument(activeXComponent, source, false, true);
//转换
Dispatch.call(doc, cmd, attributes);
} catch (Exception e) {
LOGGER.error("Word转换pdf失败!", e);
throw e;
} finally {
destroy(activeXComponent, doc);
}
}
/**
* 初始化activeX组件,默认不现实
*
* @return
*/
public ActiveXComponent initActiveXComponent() {
ActiveXComponent activeXComponent = new ActiveXComponent(componentName);
activeXComponent.setProperty("Visible", Variant.VT_FALSE);
// 禁用宏
activeXComponent.setProperty("AutomationSecurity", new Variant(3));
return activeXComponent;
}
/**
* 初始化activeX组件,并显示打开
*
* @return
*/
public ActiveXComponent initActiveXComponentVisible() {
ActiveXComponent activeXComponent = new ActiveXComponent(componentName);
activeXComponent.setProperty("Visible", Variant.VT_TRUE);
return activeXComponent;
}
/**
* 关闭文档
* 关闭参数说明:
* 0 不保存更改直接关闭文档
* -2 提示保存
* -1 保存更改并关闭
*/
public void close(Dispatch activeDocument) {
if (activeDocument != null) {
Dispatch.call(activeDocument, "Close", new Variant(0));
}
}
/**
* 结束进程
*
* @param activeXComponent activeX组件
*/
public void quit(ActiveXComponent activeXComponent) {
if (activeXComponent != null) {
Dispatch.call(activeXComponent, "Quit");
}
}
/**
* 资源销毁
*
* @param activeXComponent activeX组件
* @param activeDocument 活动文档
*/
public void destroy(ActiveXComponent activeXComponent, Dispatch activeDocument) {
try {
//关闭打开的文档
close(activeDocument);
} catch (Exception e) {
LOGGER.error("jacob关闭文档失败!", e);
} finally {
try {
quit(activeXComponent);
} catch (Exception e) {
LOGGER.error("jacob结束office进程失败!", e);
} finally {
ComThread.Release();
}
}
}
private static final Logger LOGGER = LoggerFactory.getLogger(MSOfficeApplication.class);
private String componentName;
MSOfficeApplication(String componentName) {
this.componentName = componentName;
}
public String getComponentName() {
return componentName;
}
}