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.word转html

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.Excel转html

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. sed原理及使用

    前言 环境:centos6.5 sed版本:GNU sed version 4.2.1 本文的代码都是在这个环境下验证的. 一.简介 sed(Stream Editor)意为流编辑器,是Unix常见的 ...

  2. [leetcode]53. Maximum Subarray最大子数组和

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  3. 关于requests库中文编码问题

    转自:代码分析Python requests库中文编码问题 Python reqeusts在作为代理爬虫节点抓取不同字符集网站时遇到的一些问题总结. 简单说就是中文乱码的问题.   如果单纯的抓取微博 ...

  4. powerDesigner 正向工程生成sql注释

    找到script-->objects-->column-->add value内容如下: %:COLUMN% %:DATATYPE%[.Z:[%Compressed%? compre ...

  5. 去掉手机端延迟300ms

    手机端300ms延迟是由于在手机上可以双击可以放大缩小造成的,当初ios苹果的工程师们做了一些约定,应对 iPhone 这种小屏幕浏览桌面端站点的问题.这就是手机端300ms延迟的由来. 解决:我是用 ...

  6. /bin/bash: /bin/java: Is a directory 解决

    Hadoop执行 mapreduce报错 -- ::, INFO mapreduce.Job: map % reduce % -- ::, INFO mapreduce.Job: Job job_15 ...

  7. Shell脚本-基本运算符

    跟着RUNOOB网站的教程学习的笔记 shell和其他编程语言一样,支持多种运算符,包括: 算术运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 expr是一款表达式计算公式,使用它能完成 ...

  8. JavaScript基础视频教程总结(031-040章)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. 安装php7.2并且整合nginx且分开部署

    1.安装php 7.2 2.php配置 3.nginx配置 4.测试 5.报错与解决 6.利用upstream实现负载均衡 1.安装php 7.2 启动容器: liwangdeMacBook-Air: ...

  10. [ 9.26 ]CF每日一题系列—— 771B递推问题

    Description: 给定你命名的规律,1-10个字符,开头必须大写,最多有50个名字,然后告诉你有n个人,判断区间长度为k,那么你将得到n - k + 1个答案(YES or NO) 表示1 - ...