运行条件:JDK + jacob.jar + jacob.dll

1) 把jacob.dll在 JAVA_HOME\bin\ 和 JAVA_HOME\jre\bin\ 以及C:\WINDOWS\system32\目录下各放一份
2) 把jacob.jar放入 项目的lib包下,并且在“java构建路径”中也要加载此jar包。
3) 运行项目即可编译通过.

注:jacob.jar以及jacob.dll版本要和jdk版本相匹配,否则可能出现异常!

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.*; public class OfficeToHTML { private final static OfficeToHTML oOfficeToHTML = new OfficeToHTML(); public static OfficeToHTML getInstance() {
return oOfficeToHTML;
} public OfficeToHTML() {
} public boolean WordtoHtml(String s, String s1) {
ComThread.InitSTA();
ActiveXComponent activexcomponent = new ActiveXComponent(
"Word.Application");
String s2 = s;
String s3 = s1;
boolean flag = false;
try {
activexcomponent.setProperty("Visible", new Variant(false));
Dispatch dispatch = activexcomponent.getProperty("Documents").toDispatch();
Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", ,
new Object[] { s2, new Variant(false), new Variant(true) },
new int[]).toDispatch();
Dispatch.invoke(dispatch1, "SaveAs", , new Object[] { s3,
new Variant() }, new int[]);
Variant variant = new Variant(false);
Dispatch.call(dispatch1, "Close", variant);
flag = true;
} catch (Exception exception) {
exception.printStackTrace();
} finally {
activexcomponent.invoke("Quit", new Variant[]);
ComThread.Release();
ComThread.quitMainSTA();
}
return flag;
} public boolean PPttoHtml(String s, String s1) {
ComThread.InitSTA();
ActiveXComponent activexcomponent = new ActiveXComponent(
"PowerPoint.Application");
String s2 = s;
String s3 = s1;
boolean flag = false;
try {
Dispatch dispatch = activexcomponent.getProperty("Presentations")
.toDispatch();
Dispatch dispatch1 = Dispatch.call(dispatch, "Open", s2,
new Variant(-), new Variant(-), new Variant())
.toDispatch();
Dispatch.call(dispatch1, "SaveAs", s3, new Variant());
Variant variant = new Variant(-);
Dispatch.call(dispatch1, "Close");
flag = true;
} catch (Exception exception) {
System.out.println("|||" + exception.toString());
} finally {
activexcomponent.invoke("Quit", new Variant[]);
ComThread.Release();
ComThread.quitMainSTA();
}
return flag;
} public boolean ExceltoHtml(String s, String s1) {
ComThread.InitSTA();
ActiveXComponent activexcomponent = new
ActiveXComponent("Excel.Application");
String s2 = s;
String s3 = s1;
boolean flag = false;
try
{
activexcomponent.setProperty("Visible", new Variant(false));
Dispatch dispatch =
activexcomponent.getProperty("Workbooks").toDispatch();
Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", , new
Object[] {
s2, new Variant(false), new Variant(true)
}, new int[]).toDispatch();
Dispatch.call(dispatch1, "SaveAs", s3, new Variant());
Variant variant = new Variant(false);
Dispatch.call(dispatch1, "Close", variant);
flag = true;
}
catch(Exception exception)
{
System.out.println("|||" + exception.toString());
}
finally
{
activexcomponent.invoke("Quit", new Variant[]);
ComThread.Release();
ComThread.quitMainSTA();
}
return flag;
} public static void main(String args[]) {
OfficeToHTML otx = OfficeToHTML.getInstance();
boolean flag1 = otx.PPttoHtml("e:/test/test3.pptx", "e:/test/test3.html");
if(flag1){
System.out.println("PPT文件转换成HTML成功!");
}else{
System.out.println("PPT文件转换成HTML失败!");
}
}
}

Java中Office(word/ppt/excel)转换成HTML实现的更多相关文章

  1. 在Java中导出word、excel格式文件时JSP页面头的设置

    我们在JSP中往往会把一些表格里的东西需要导出到本地,一般都是导成word.excel格式的文件.这只需要在JSP页面头设置及在<head></head>标签中添加下面的代码: ...

  2. Java中如何将字符串数组转换成字符串

    如果将“字符串数组”转换成“字符串”,只能通过循环,没有其他方法: public static String getExecSqlString(String str){ StringBuffer sb ...

  3. Java中为什么long能自动转换成float类型

    刷题时候看到一个float和long相互转换的问题,float向long转换的时候不会报错,一个4个字节一个8个字节,通过baidu找到了答案. 下面转载自http://blog.csdn.net/s ...

  4. Java中如何把一下字符串转换成map

    首先,你先确认你的字符串是否是json格式的,如果是json格式,那你可以使用Gson.jar或json-lib-xx-jdk.jar两个包来自动解析解析. 使用Gson更简单些,只需要导入一个包就可 ...

  5. java中如何将字符串数组转换成字符串(转)

    如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"} ...

  6. Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

    Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...

  7. word ppt excel文档转换成pdf

    1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...

  8. JAVA:借用OpenOffice将上传的Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

  9. office(Word、Excel、PPT等图标异常和桌面无新建解决方案)

    前言吐槽: 前不久因为安装了WPS,然后觉得不好用卸载WPS装回office就出现了一个很恶心的问题:word文档.excel文档.PPT这些办公软件图标异常,显示的是下面这样: 打开倒是可以正常用w ...

随机推荐

  1. sax 解析 xml

    SAX(Simple API for XML) https://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html persons.xml &l ...

  2. 任意List 和DatabTable的转换

     public static IEnumerable<T> ToEntityList<T>(this DataTable table) where T : class      ...

  3. JSP教程推荐

    从搭建环境到上手JSP再到Servlet. http://www.runoob.com/jsp/jsp-tutorial.html

  4. 09-FZ6R 白色

  5. Qt出现cannot find -IGL错误

    Solution: sudo apt-get install build-essential sudo apt-get install libgl1-mesa-dev

  6. linux挂载远程samba目录

    yum install cifs-utils  #安装cifs协议包 #列出远程目录 smbclient -L 192.100.9.165 -Uadministrator vim /etc/fstab ...

  7. iOS应用第三方推送的添加

    现在的一些第三方的推送平台挺好用,主要是因为他们有类似微信公众平台一样的管理后台,简单易用,封装了很多开发者需要的推送功能. 下面以个推为例: 1.在个推的应用配置iOS部分设置自己的BounleID ...

  8. C++ essentials 之 static 关键字

    extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...

  9. ( 译、持续更新 ) JavaScript 上分小技巧(三)

    最近家里杂事较多,自学时间实在少的可怜,所以都在空闲时间看看老外写的内容,学习之外顺便翻译分享~等学习的时间充足些再写写自己的一些学习内容和知识点分析(最近有在接触的:复习(C#,SQL).(学习)T ...

  10. geoip scala api

    #!/bin/bash /home/hadoop/spark-1.6.2/bin/spark-shell --master spark://hbase11:7077 --executor-memory ...