jacobUtil

 package utils;

import org.apache.log4j.Logger;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JacobUtil {
static Logger logger = Logger.getLogger(JacobUtil.class);
public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int ppSaveAsJPG = 17;
public static final int EXCEL_HTML = 44;
/**
* WORD转HTML
* @param docfile
*            WORD文件全路径
* @param htmlfile
*            转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile) {
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application"); // 启动word
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch
.invoke(
docs,
"Open",
Dispatch.Method,
new Object[]{docfile, new Variant(false),
new Variant(true)}, new int[1])
.toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{
htmlfile, new Variant(WORD_HTML)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (Exception e) {
logger.error(e);
logger.error("文件名:" + docfile + ",处理后文件名:" + htmlfile);
ComThread.Release();
} finally {
logger.info("转化成功");
app.invoke("Quit", new Variant[]{});
// 释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
ComThread.Release();
}
}
/**
* EXCEL转HTML
* @param xlsfile
*            EXCEL文件全路径
* @param htmlfile
*            转换后HTML存放路径
*/
public static void excelToHtml(String xlsfile, String htmlfile) {
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Excel.Application"); // 启动Excel
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch
.invoke(
excels,
"Open",
Dispatch.Method,
new Object[]{xlsfile, new Variant(false),
new Variant(true)}, new int[1])
.toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[]{
htmlfile, new Variant(EXCEL_HTML)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
logger.error(e);
logger.error("文件名:" + xlsfile + ",处理后文件名:" + htmlfile);
ComThread.Release();
} finally {
logger.info("转化成功");
app.invoke("Quit", new Variant[]{});
// 释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
ComThread.Release();
}
}
/**
* 将ppt转化为图片
* @param pptfile
* @param saveToFolder
* @date 2009-7-4
* @author YHY
*/
public static void PPTToJPG(String pptfile, String saveToFolder) {
ActiveXComponent ppt = null;
ActiveXComponent presentation = null;
try {
// 新建一个powerpoint程序实例
ppt = new ActiveXComponent("PowerPoint.Application");
// 设置程序界面是否可见
ppt.setProperty("Visible", new Variant(true));
ActiveXComponent presentations = ppt
.getPropertyAsComponent("Presentations");
// 打开一个现有的 Presentation 对象
presentation = presentations.invokeGetComponent("Open",
new Variant(pptfile), new Variant(true));
saveAs(presentation, saveToFolder, ppSaveAsJPG);
if (presentation != null) {
Dispatch.call(presentation, "Close");
}
} catch (Exception e) {
logger.error(e);
ComThread.Release();
} finally {
if (presentation != null) {
Dispatch.call(presentation, "Close");
}
ppt.invoke("Quit", new Variant[]{});
ComThread.Release();
}
}
/**
* 播放ppt
* @param pptFile
* @date 2009-7-4
* @author YHY
*/
public static void PPTShow(String pptFile) {
// 新建一个powerpoint程序实例
ActiveXComponent ppt = new ActiveXComponent("PowerPoint.Application");
// 设置程序界面是否可见
ppt.setProperty("Visible", new Variant(true));
ActiveXComponent presentations = ppt
.getPropertyAsComponent("Presentations");
// 打开一个现有的 Presentation 对象
ActiveXComponent presentation = presentations.invokeGetComponent(
"Open", new Variant(pptFile), new Variant(true));
// powerpoint幻灯展示设置对象
ActiveXComponent setting = presentation
.getPropertyAsComponent("SlideShowSettings");
// 调用该对象的run函数实现全屏播放
setting.invoke("Run");
// 释放控制线程
ComThread.Release();
}
public static void fileToHtml(String filename, String htmlfile) {
String ext = filename.substring(filename.lastIndexOf(".") + 1)
.toLowerCase();
if ("doc".equals(ext)) {
wordToHtml(filename, htmlfile);
} else if ("xls".equals(ext)) {
excelToHtml(filename, htmlfile);
} else {
logger.error("文件格式有误!");
return;
}
}
/**
* ppt另存为
* @param presentation
* @param saveTo
* @param ppSaveAsFileType
* @date 2009-7-4
* @author YHY
*/
public static void saveAs(Dispatch presentation, String saveTo,
int ppSaveAsFileType) {
try {
Object slides = Dispatch.get(presentation, "Slides").toDispatch();
Dispatch.call(presentation, "SaveAs", saveTo, new Variant(
ppSaveAsFileType));
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值