Java-jacob-文件转HTML
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的更多相关文章
- java创建文件和目录
java创建文件和目录 2013-09-04 12:56 99933人阅读 评论(7) 收藏 举报 分类: JAVA基础(10) 版权声明:本文为博主原创文章,未经博主允许不得转载. 创建文件和目 ...
- java之文件基本操作
java之文件基本操作 1 使用 BufferedReader 在控制台读取字符 public static void readChar() throws IOException{ char c; I ...
- java中文件的I/O操作
java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...
- java进行文件上传,带进度条
网上看到别人发过的一个java上传的代码,自己写了个完整的,附带源码 项目环境:jkd7.tomcat7. jar包:commons-fileupload-1.2.1.jar.commons-io-1 ...
- Java将文件转为字节数组
Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列 ...
- Java的文件读写操作
file(内存)----输入流---->[程序]----输出流---->file(内存) 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStr ...
- java通过文件路径读取该路径下的所有文件并将其放入list中
java通过文件路径读取该路径下的所有文件并将其放入list中 java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...
- Java Class文件详解
作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) Java Class文件中包含以下信息: [+]view code ClassFile { u4 magic; ...
- Java api 入门教程 之 JAVA的文件操作
I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...
- 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
建立好一个Maven项目后,如果Java Resources资源文件下没有src/main/java文件夹,并且在手动创建这个文件时提示“已存在文件”. 这说明,在这个项目配置中已经有了src/m ...
随机推荐
- HDU2028
#include <bits/stdc++.h> using namespace std; ; int gcd(int a, int b) { ? b:gcd(b, a%b); } int ...
- 2,postman的tests的断言写法
tests的断言主要是分为三类 状态码,header内容和波body内容的测试,波body的不常用( 不容易控制) pm.expect(pm.response).to.have.status(&quo ...
- redis在游戏服务器中的使用初探(四) redis应用
文章系列先介绍环境搭建 介绍redis操作和代码编写运行 这是典型的实战工程过程.那么我们为何要使用redis而不是常规的数据库比如 mysql呢? 因为KV内存数据库最大的优势所有数据全部存储在内 ...
- playframework 一步一步来 之 日志 (二)
带着之前的疑问,我们先回顾一下日志相关的知识: 首先是SL4J,SL4J是个什么东西来着?官方解释为:“The Simple Logging Facade for Java (SLF4J) serve ...
- (22)Embrace the near win
https://www.ted.com/talks/sarah_lewis_embrace_the_near_win/transcript?referrer=playlist-talks_to_get ...
- Win7 VS2017编译bgfx图形API
官方的编译指南在这个页面 https://bkaradzic.github.io/bgfx/build.html#quick-start 目前的版本编译比较简单,下载3个项目,放于同级目录下 http ...
- python调用 sshpass
[root@qinhan ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ine ...
- ScriptOJ-safeGet#99
const safeGet = (data, path) => { if(!path) return undefined; const pathArr = path.split('.'); le ...
- 第一周Access课总结
一.问:这节课学到了什么知识? 答:这周课程迎来新的学习领域,作为初次学Access有了一定的了解,Access是office办公软件中的一个极为重要的组成部分,它可以对大量的数据进行存储,查找,统计 ...
- 《pyhton语言程序设计》_第一章笔记
章1.62 (1).python区分大小写. (2).python忽略在符号#之后的同行的内容 (3).python和matlab很相似(个人感觉) (4).章节1.91: >>>i ...