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. JVM思考-ClassLoader.loadClasshe和Class.forName区别

    JVM思考-ClassLoader.loadClasshe和Class.forName区别 目录:JVM总括:目录 见博客第四节:JVM总括四-类加载过程.双亲委派模型.对象实例化过程

  2. Python 多个分隔符 读取逗号和空格分开的数据

    str.split()  清除默认 空格和tab  对空格数量不敏感 str.split(' ') 只清除一个空格     对空格数量敏感 l = re.split('[^0-9.]+',s.stri ...

  3. redis持久化详述

    本来打算根据自己搜索的一些文章写些总结,后来发现了一篇好文,这里转载下,在自己博客里面记录下. 原文链接:https://www.cnblogs.com/kismetv/p/9137897.html ...

  4. [JAVA]JAVA章4 Thread Dump如何分析

    一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工具.每一个Java虚拟机都有及时生成所有线程在某一点状态的thread- ...

  5. node.js获取参数的常用方法

    1.req.body 2.req.query 3.req.params 一.req.body例子 body不是nodejs默认提供的,你需要载入body-parser中间件才可以使用req.body, ...

  6. 2019.02.11 bzoj4818: [Sdoi2017]序列计数(矩阵快速幂优化dp)

    传送门 题意简述:问有多少长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数,且其中至少有一个数是质数,答案对201704082017040820170408取模(n≤1e9, ...

  7. 安卓逆向学习---深入Smali文件

    参考:https://www.52pojie.cn/thread-396966-1-1.html Smali中的包信息 .class public Lcom/aaaaa; //他是com.aaaaa这 ...

  8. 在Unity5.6.5f1中使用C#7语法

    备忘,记忆力越来越差了,必需把这种琐碎的东西记下来,以防1年后想再用完全没头绪. 之前试过用C#6语法,但是怎么配置操作的完全没印象了. 首先去这下载扩展 https://bitbucket.org/ ...

  9. css概括2

    Css内容: 常用样式:字体.颜色.背景... 字体:大小.颜色.粗细.字体 Text-decoration:文本修饰{overline 上 Underline 下 Line-throung 中} T ...

  10. UEditor的jQuery插件化 -转

    UEditor本身并不依赖jQuery,但如果在项目中同时使用两者的话,可能会希望使用jQuery语法创建和获取编辑器实例.为此,需要为jQuery编写插件,代码如下: (function ($) { ...