一、把jacob-1.19.jar放在lib
二、jacob-1.19-x64.dll放在jdk的bin、jre的bin(一共三个地方)
三、把JacobUtils.java放在工具类,直接调用就行。
JacobUtils.java 如下:
package com.fh.util;
import java.io.IOException;
import java.util.Scanner;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* 使用本地office安装包进行文件转换操作
* @author Administrator
*
*/
public class JacobUtils {
private JacobUtils(){}
/**
* 将word转为HTML
* @param filename
* @param htmlFilename
* @return
*/
public static boolean wordToHtml(String filename, String htmlFilename) {
ActiveXComponent xl = null;
boolean rel = false;
//打开一个word,不显示窗口
try {
xl = new ActiveXComponent("Word.Application");
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);
rel = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 调用office关闭方法,关闭窗口和word进程
xl.invoke("Quit", new Variant[] {});
xl = null;
}
return rel;
}
/**
* 将excel转为HTML
* @param filename
* @param htmlFilename
* @return
*/
public static boolean excelToHtml(String filename, String htmlFilename) {
ActiveXComponent xl = null;
boolean rel = false;
try {
xl = new ActiveXComponent("Excel.Application");
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");
rel = true;
} 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) {
rel = false;
e.printStackTrace();
}
}
return rel;
}
/**
* 将ppt转为HTML
* @param filename
* @param htmlFilename
* @return
*/
public static boolean pptToHtml(String filename, String htmlFilename) {
ActiveXComponent xl = null;
boolean rel = false;
try {
xl = new ActiveXComponent("Powerpoint.Application");
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的加这两行会报错,干脆注释上,反正在下面也关闭进程
rel = true;
} 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) {
rel = false;
e.printStackTrace();
}
}
return rel;
}
}
四、调用:
@RequestMapping(value="/tohtml")
public ModelAndView tohtml()throws Exception{
ModelAndView mv = this.getModelAndView();
String filename="D:\\11.doc";
String htmlFilename="D:\\test.html";
JacobUtils.wordToHtml(filename, htmlFilename);
return mv;
}
五、三个附件:https://download.csdn.net/download/weixin_38948287/10760100
链接: https://pan.baidu.com/s/10AWA7olu2ar-sY-Z5AQhag
提取码: jnqs 复制这段内容后打开百度网盘手机App,操作更方便哦