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 ...
随机推荐
- 删除centos7中自带有python2.7
删除centos7中自带有python2. ()强制删除已安装python及其关联 # rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ...
- 什么叫做API?看完你就理解了
阅读编程资料时经常会看到API这个名词,网上各种高大上的解释估计放倒了一批初学者.初学者看到下面这一段话可能就有点头痛了. API(Application Programming Interface, ...
- ES6 Rest参数
Rest参数接收函数的多余参数,组成一个数组,放在形参的最后,形式如下: function func(a, b, ...theArgs) { // ... } rest参数只包括那些没有给出名称的参数 ...
- FastFDS安装及简单使用
1. FastDFS安装(ubuntu) 需要准备: nginx.fastdfs.libfastcommon.gcc.git apt-get update apt-get -y install mak ...
- mysql找到数据的存储位置
本来是想找mysql数据库文件中的sql脚本文件的,结果发现运行了sql脚本文件后,你删除了,就没有sql语句了,那么我们分析一下在数据库路径下面找到的文件又是什么呢? 1.先找mysql中data的 ...
- SpringMvc在返回数据之前进行统一处理
这里其实有多种解决方案 如果你不需要获取request对象 可以采用aop(环绕通知)的方式来统一修改 如果你需要获取request对象,那么就需要采用下面的方式 0自己定义一个注解,内容如下 @Ta ...
- JS基础-分支结构-循环-数组
1.分支结构 1.if结构 语法: if(条件){ 语句块: } 注意: 1.条件尽量是boolean的,如果不是boolean的,以下条件值,会当做f ...
- poj3130 (半平面交
题意:判断是否存在内核. 半平面交存板子. /* gyt Live up to every day */ #include<cstdio> #include<cmath> #i ...
- Ubuntu 18.04 Server上安装LAMP
由于要进行渗透测试,所以这两天就在搭LAMP的环境(过程及其痛苦) 这里分享一些我遇到的问题. 首先介绍一下我的使用环境 VM虚拟机,ubuntu 与主机NAT连接 由于之前一直使用的是kali(默 ...
- NAT 模式下虚拟机安装的centos7 ping主机显示connect: Network is unreachable
在虚拟机下安装的centos7使用的网络是NAT模式,安装成功后ping主机地址显示 Network is unreachable 解决方案: 1)使用ifconfig命令查看网卡信息 2)进入/et ...