常用文档格式转换,doc转htm,excel转htm, doc转pdf格式以便查阅。
要求:
1、需要依赖jacob-1.14.3.jar
2、jacob-1.14.3-x64.dll 【如果编译报错缺少缺少该文件,需要将该文件放入 x://windows/system32目录下,然后将该文件复制到jdk的lib目录下,才可以正常编译,谢谢。】
package com.sf.test;
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class DocUtil implements Runnable {
private static final String LOCK = "lock";
/** 转换原文件 */
private String fromFile;
/** 转换后文件 */
private String toFile;
/** 转换类型 */
private String transType;
/**
* 将doc文件转换成htm文件
*
* @Time May 4, 2009 6:11:54 PM create
* @param fileName
* 文件名称
* @param viewPath
* 访问相对路径
* @throws Exception
* @return String htm文件名称,包含访问路径
* @author letuoyu
*/
public static String docToHtm(String fileName) {
String hf = "";
if (fileName.endsWith(".docx")) {
hf = fileName.replaceAll(".docx", ".htm");
} else if (fileName.endsWith(".doc")) {
hf = fileName.replaceAll(".doc", ".htm");
}
DocUtil util = new DocUtil();
util.fromFile = fileName;
util.toFile = hf;
util.transType = "doc";
new Thread(util).start();
return hf;
}
/**
* excel转htm文件
* @param fileName
* @return
*/
public static String excelToHtm(String fileName) {
String hf = "";
if (fileName.endsWith(".xlsx")) {
hf = fileName.replaceAll(".xlsx", ".htm");
} else if (fileName.endsWith(".xls")) {
hf = fileName.replaceAll(".xls", ".htm");
}
DocUtil util = new DocUtil();
util.fromFile = fileName;
util.toFile = hf;
util.transType = "excel";
new Thread(util).start();
return hf;
}
/**
* 将doc文件转换成pdf文件
*
* @Time May 4, 2009 6:11:54 PM create
* @param fileName
* 文件名称
* @param viewPath
* 访问相对路径
* @throws Exception
* @return String htm文件名称,包含访问路径
* @author zhangwei
*/
public static String docToPdf(String fileName) throws Exception {
String hf = "";
if (fileName.endsWith(".docx")) {
hf = fileName.replaceAll(".docx", ".pdf");
} else if (fileName.endsWith(".doc")) {
hf = fileName.replaceAll(".doc", ".pdf");
}
DocUtil util = new DocUtil();
util.fromFile = fileName;
util.toFile = hf;
util.transType = "pdf";
new Thread(util).start();
return hf;
}
@Override
public void run() {
try {
synchronized (LOCK) {
if ("doc".equals(this.transType)) {
this.word2Html(fromFile, toFile);
} else if ("pdf".equals(transType)) {
this.word2Pdf(fromFile, toFile);
} else if ("excel".equals(transType)) {
this.excel2Html(fromFile, toFile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Time May 4, 2009 5:09:01 PM create
* @param docfile
* 传入的doc文件,包含绝地路径
* @param htmlfile
* 需要转成的htm文件,包含绝地路径
* @throws Exception
* @author letuoyu
*/
private void word2Html(String docfile, String htmlfile) throws Exception {
ActiveXComponent app = null;
ComThread.InitSTA();// 初始化com的线程,非常重要!!
try {
String _docFilePath = new File(docfile).getCanonicalPath();
String _htmlFilePath = new File(htmlfile).getCanonicalPath();
// 启动word
app = new ActiveXComponent("Word.Application");
// 设置word不可见
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
// 打开word文件
Dispatch doc = Dispatch
.invoke(docs, "Open", Dispatch.Method,
new Object[] { _docFilePath, new Variant(false), new Variant(true) }, new int[1])
.toDispatch();
// 作为html格式保存到临时文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { _htmlFilePath, new Variant(8) }, new int[1]);
Variant f = new Variant(false);
if (doc.getProgramId() != null) {
Dispatch.call(doc, "Close", f);
}
} catch (ComFailException e) {
throw e;
} catch (Exception e) {
throw e;
} finally {
if (app != null) {
app.invoke("Quit", new Variant[] {});
}
app = null;
// 释放com线程
ComThread.Release();
}
}
/**
* excel转html文件
* @param excelfile
* @param htmlfile
* @throws Exception
*/
private void excel2Html(String excelfile, String htmlfile) throws Exception {
ActiveXComponent app = null;
ComThread.InitSTA();// 初始化com的线程,非常重要!!
try {
String _docFilePath = new File(excelfile).getCanonicalPath();
String _htmlFilePath = new File(htmlfile).getCanonicalPath();
// 启动word
app = new ActiveXComponent("Excel.Application");
// 设置word不可见
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Workbooks").toDispatch();
// 打开word文件
Dispatch doc = Dispatch
.invoke(docs, "Open", Dispatch.Method,
new Object[] { _docFilePath, new Variant(false), new Variant(true) }, new int[1])
.toDispatch();
// 作为html格式保存到临时文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { _htmlFilePath, 44 }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (ComFailException e) {
throw e;
} catch (Exception e) {
throw e;
} finally {
if (app != null) {
app.invoke("Quit", new Variant[] {});
}
app = null;
// 释放com线程
ComThread.Release();
}
}
/**
* doc文件转换为pdf文件
*
* @Time May 4, 2009 5:09:01 PM create
* @param filename
* 传入的doc文件,包含绝地路径
* @param toFilename
* 需要转成的pdf文件,包含绝地路径
* @throws Exception
* @author zhangwei
*/
private void word2Pdf(String filename, String toFilename) {
int wdDoNotSaveChanges = 0;// 不保存待定的更改。
int wdFormatPDF = 17;// PDF 格式
ActiveXComponent app = null;
try {
String _docFilePath = new File(filename).getCanonicalPath();
String _pdfFilePath = new File(toFilename).getCanonicalPath();
System.out.println("启动Word...");
long start = System.currentTimeMillis();
ComThread.InitSTA();
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
System.out.println("打开文档:" + _docFilePath);
Dispatch doc = Dispatch.call(docs, //
"Open", //
_docFilePath, // FileName
false, // ConfirmConversions
true // ReadOnly
).toDispatch();
System.out.println("转换文档到PDF..." + _pdfFilePath);
File tofile = new File(_pdfFilePath);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, //
"SaveAs", //
_pdfFilePath, // FileName
wdFormatPDF);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e);
} finally {
if (app != null) {
try {
app.invoke("Quit", wdDoNotSaveChanges);
} catch (Exception e) {
e.printStackTrace();
}
}
ComThread.Release();
}
}
public static void main(String[] arg) {
//docToHtm("d://util/顺丰同城LSS产品需求说明书(6月).docx");
//excelToHtm("d://util/LSS6月版本功能清单.xlsx");
try {
docToPdf("d://util/xxxx需求说明书(6月).docx");
} catch (Exception e) {
e.printStackTrace();
}
}
}