Java-jacob-文件转HTML:

下载jacob的jar包,然后举个例子。

	public static final int WORD_HTML = 8;

	public static final int WORD_TXT = 7;

	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 = new ActiveXComponent("Word.Application"); // 启动word
try {
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) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
} /**
* EXCEL转HTML
*
* @param xlsfile
* EXCEL文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static void excelToHtml(String xlsfile, String htmlfile) {
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word
try {
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) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
} public static void main(String[] args) {
excelToHtml(
"D:\\work\\apache-tomcat-6.0.36\\webapps\\ReportSystem\\upload\\1374758916167.xls",
"D:\\work\\apache-tomcat-6.0.36\\webapps\\ReportSystem\\upload\\1374758916167.html");
}

Jacob把Word Excel PPT转成Html

首先,必须将jacob-1.15-M3-x86.dll这个文件(视版本而定,在32位xp系统中使用该文件,在64位系统中,如win7,就得使用该文件jacob-1.15-M3-x64.dll了)拷贝到C:\Program Files\Java\jdk1.6.0_04\jre\bin的目录下,然后将jacob.jar在添加到C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib的目录下,在项目中导入jacob.jar即可使用下面程序:

1.wordhtml

Word转起来还是比较容易的,不会出什么问题,转换之后进程自动关闭。

public class Word2Html {

public static void change(String filename, String htmlFilename) {

ActiveXComponent xl = new ActiveXComponent("Word.Application");

//打开一个word,不显示窗口

try {

Dispatch.put(xl, "Visible", new Variant(false));

Object workbooks = xl.getProperty("Documents").toDispatch();

Object workbook = Dispatch.call((Dispatch) workbooks, "Open",

filename).toDispatch();

Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,

new Object[] { htmlFilename, new Variant(8) }, new int[1]);

Variant f = new Variant(false);

//Close关闭文件,不关闭窗口

Dispatch.call((Dispatch) workbooks, "Close", f);

} catch (Exception e) {

e.printStackTrace();

} finally {

// 调用office关闭方法,关闭窗口和word进程

xl.invoke("Quit", new Variant[] {});

xl = null;

}

}

public static void main(String[] args) {

Word2Html.change("d:/a.doc", "d:/a");

}

}

因为word转html之后,word进程自动关闭,所以在finally中不写关闭进程代码,但在excel和ppt转换后进程不会自动关闭,要加上关闭进程代码。

2.Excelhtml

Excel最头疼的就是转换完之后,进程不会释放,而且每一次转换都会加一个进程,崩溃!!

无奈之下不得不在finally中杀掉excel进程。

public class Excel2Html {

public static void change(String filename, String htmlFilename) {

ActiveXComponent xl = new ActiveXComponent("Excel.Application");

try {

Dispatch.put(xl, "Visible", new Variant(false));

//打开一个Excel,不显示窗口

Object workbooks = xl.getProperty("workbooks").toDispatch();

Object workbook = Dispatch.call((Dispatch) workbooks, "Open",

filename).toDispatch();

Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,

new Object[] { htmlFilename, new Variant(44) }, new int[1]);

Dispatch.call((Dispatch) workbooks, "Close");

} catch (Exception e) {

e.printStackTrace();

} finally {

xl.invoke("Quit", new Variant[] {});

xl = null;

Process process;

int pid = 0;

try {

process = Runtime.getRuntime().exec("tasklist");

Scanner in = new Scanner(process.getInputStream());

while (in.hasNextLine()) {

String p = in.nextLine();

// 打印所有进程

System.out.println(p);

if (p.contains("EXCEL.EXE")) {

StringBuffer buf = new StringBuffer();

for (int i = 0; i < p.length(); i++) {

char ch = p.charAt(i);

if (ch != ' ') {

buf.append(ch);

}

}

// 打印pid,根据pid关闭进程

System.out.println(buf.toString().split("Console")[0]

.substring("EXCEL.EXE".length()));

pid = Integer.parseInt(buf.toString().split("Console")[0]

.substring("EXCEL.EXE".length()));

Runtime.getRuntime().exec("tskill"+" "+pid);

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) {

Excel2Html.change("d:/b.xls", "d:/b");

}

}

3.PPT转html

PPT转html最头疼的就是new Variant(false)会报错,无奈只能设成true,然后在finally中杀掉powerpnt进程。

public class Ppt2Html {

public static void change(String filename, String htmlFilename) {

ActiveXComponent xl = new ActiveXComponent("Powerpoint.Application");

try {

Dispatch.put(xl, "Visible", new Variant(true));

//打开一个PPT,显示窗口,PPT的不显示就会报错,狂晕!

Object workbooks = xl.getProperty("Presentations").toDispatch();

Object workbook = Dispatch.call((Dispatch) workbooks, "Open",

filename).toDispatch();

Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,

new Object[] { htmlFilename, new Variant(20) }, new int[1]);

//Variant f = new Variant(false);

//Dispatch.call((Dispatch) workbooks, "Close",f);

//PPT的加这两行会报错,干脆注释上,反正在下面也关闭进程

} catch (Exception e) {

e.printStackTrace();

} finally {

xl.invoke("Quit", new Variant[] {});

xl = null;

Process process;

int pid = 0;

try {

process = Runtime.getRuntime().exec("tasklist");

Scanner in = new Scanner(process.getInputStream());

while (in.hasNextLine()) {

String p = in.nextLine();

// 打印所有进程

System.out.println(p);

if (p.contains("POWERPNT.EXE")) {

StringBuffer buf = new StringBuffer();

for (int i = 0; i < p.length(); i++) {

char ch = p.charAt(i);

if (ch != ' ') {

buf.append(ch);

}

}

// 打印pid,根据pid关闭进程

System.out.println(buf.toString().split("Console")[0]

.substring("POWERPNT.EXE".length()));

pid = Integer

.parseInt(buf.toString().split("Console")[0]

.substring("POWERPNT.EXE".length()));

Runtime.getRuntime().exec("tskill" + " " + pid);

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) {

Ppt2Html.change("d:/c.ppt", "d:/c");

}

}

Java-jacob-文件转HTML的更多相关文章

  1. java创建文件和目录

    java创建文件和目录 2013-09-04 12:56 99933人阅读 评论(7) 收藏 举报  分类: JAVA基础(10)  版权声明:本文为博主原创文章,未经博主允许不得转载. 创建文件和目 ...

  2. java之文件基本操作

    java之文件基本操作 1 使用 BufferedReader 在控制台读取字符 public static void readChar() throws IOException{ char c; I ...

  3. java中文件的I/O操作

    java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...

  4. java进行文件上传,带进度条

    网上看到别人发过的一个java上传的代码,自己写了个完整的,附带源码 项目环境:jkd7.tomcat7. jar包:commons-fileupload-1.2.1.jar.commons-io-1 ...

  5. Java将文件转为字节数组

    Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列 ...

  6. Java的文件读写操作

    file(内存)----输入流---->[程序]----输出流---->file(内存) 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStr ...

  7. java通过文件路径读取该路径下的所有文件并将其放入list中

    java通过文件路径读取该路径下的所有文件并将其放入list中   java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...

  8. Java Class文件详解

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) Java Class文件中包含以下信息: [+]view code ClassFile { u4 magic;  ...

  9. Java api 入门教程 之 JAVA的文件操作

    I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...

  10. 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法

      建立好一个Maven项目后,如果Java Resources资源文件下没有src/main/java文件夹,并且在手动创建这个文件时提示“已存在文件”. 这说明,在这个项目配置中已经有了src/m ...

随机推荐

  1. Error running Tomcat8: Address localhost:xxxx is already in use

    参考自: https://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683 第一步,命令提示符号,执行命令:netstat -an ...

  2. JVM总括四-类加载过程、双亲委派模型、对象实例化过程

    JVM总括四-类加载过程.双亲委派模型.对象实例化过程 目录:JVM总括:目录 一. 类加载过程 类加载过程就是将.class文件转化为Class对象,类实例化的过程,(User user = new ...

  3. 5. Redis持久化

    5. Redis持久化5.1 RDB5.1.1 触发机制5.1.2 流程说明5.1.3 RDB文件的处理5.1.4 RDB的优缺点5.2 AOF5.2.1 使用AOF5.2.2 命令写入5.2.3 文 ...

  4. Annotation 标注

    1.画出基本图 当图线中某些特殊地方需要标注时,我们可以使用 annotation. matplotlib 中的 annotation 有两种方法, 一种是用 plt 里面的 annotate,一种是 ...

  5. 简述Python入门小知识

    如今的Python开发工程师很受企业和朋友们的青睐,现在学习Python开发的小伙伴也很多,本篇文章就和大家探讨一下Python入门小知识都有哪些. 扣丁学堂简述Python入门小知识Python培训 ...

  6. apache提示make_sock?

    [root@localhost apache]# /etc/init.d/*_apache restart 停止 *_apache: [失败] 正在启动 *_apache:(98)Address al ...

  7. Android单片机与蓝牙模块通信实例代码

    Android单片机与蓝牙模块通信实例代码 参考路径:http://www.jb51.net/article/83349.htm 啦啦毕业了,毕业前要写毕业设计,需要写一个简单的蓝牙APP进行交互,通 ...

  8. Unity AssetBundle打包资源工具

    using UnityEngine;using System.Collections;using UnityEditor; /// <summary>/// 简单资源打包Editor/// ...

  9. Java 装箱和拆箱

    1.装箱机制 基础类型引用到其包装类型,这样就可以调用其各种方法. 例如,我们声明: Integer a = 1; 其在编译过程中会自动解释成: Integer a = Integer.valueOf ...

  10. 上传input中file文件到云端,并返回链接

    有的文件.图片等信息可以上传到云端上,然后使用链接调用,这样会更加的方便和快捷. <form id="form"> <input type="file& ...