jacobUtil
package jiangdu.fire.util; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class JacobUtil { // 8 代表word保存成html public static final int WORD_HTML = 8; private static final int ppSaveAsPDF = 32; private static final int EXCEL_HTML = 44; private static final int wdFormatPDF = 17; public static void main(String[] args) { String docfile = "d:\\1.doc"; String htmlfile = "d:\\1.html"; JacobUtil.wordToHtml(docfile, htmlfile); } /** * WORD转HTML * * @param docfile * WORD文件全路径 * @param htmlfile * 转换后HTML存放路径 * @return */ public static boolean wordToHtml(String docfile, String htmlfile) { ComThread.InitMTA(true); ActiveXComponent app = new ActiveXComponent("Word.Application"); 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]); Dispatch.call(doc, "Close", new Variant(false)); return true; } catch (Exception e) { //e.printStackTrace(); System.out.println("WORD文件转换后HTML异常-htmlfile-"+htmlfile); return false; } finally { app.invoke("Quit", new Variant[] {}); ComThread.Release(); } } /** * * ppt文件转成pdf * <功能详细描述> * @param pptFile ppt文件绝对路径 * @param pdfFile pdf文件绝对路径 * @return * @see [类、类#方法、类#成员] */ public static boolean ppt2PDF(String pptFile, String pdfFile) { ComThread.InitMTA(true); ActiveXComponent app = new ActiveXComponent("PowerPoint.Application"); try { Dispatch ppts = app.getProperty("Presentations").toDispatch(); Dispatch ppt = Dispatch.call(ppts, "Open", pptFile, true,// ReadOnly true,// Untitled指定文件是否有标题 false// WithWindow指定文件是否可见 ).toDispatch(); Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF); Dispatch.call(ppt, "Close"); return true; } catch (Exception e) { System.out.println("ppt文件转换后pdf异常-pdfFile-"+pdfFile); return false; } finally { app.invoke("Quit"); ComThread.Release(); } } public static boolean word2PDF(String docFile, String pdfFile) { ComThread.InitMTA(true); // 打开word应用程序 ActiveXComponent app = new ActiveXComponent("Word.Application"); try { // 设置word不可见 app.setProperty("Visible", false); // 获得word中所有打开的文档,返回Documents对象 Dispatch docs = app.getProperty("Documents").toDispatch(); // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document Dispatch doc = Dispatch.call(docs, "Open", docFile, false, true).toDispatch(); // 调用Document对象的SaveAs方法,将文档保存为pdf格式 /* * Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF //word保存为pdf格式宏,值为17 ); */ Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF // word保存为pdf格式宏,值为17 ); // 关闭文档 Dispatch.call(doc, "Close", false); // 关闭word应用程序 return true; } catch (Exception e) { System.out.println("doc文件转换后pdf异常-pdfFile-"+pdfFile); return false; } finally { app.invoke("Quit", 0); ComThread.Release(); } } /** * EXCEL转HTML * * @param xlsfile EXCEL文件全路径 * @param htmlfile 转换后HTML存放路径 * @return */ public static boolean excelToHtml(String xlsfile, String htmlfile) { // 启动excel ComThread.InitMTA(true); ActiveXComponent app = new ActiveXComponent("Excel.Application"); try { // 设置excel不可见 app.setProperty("Visible", new Variant(false)); Dispatch excels = app.getProperty("Workbooks").toDispatch(); // 打开excel文件 Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[] {xlsfile, new Variant(false), new Variant(true)}, new int[1]).toDispatch(); // 作为html格式保存到临时文件 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); return true; } catch (Exception e) { System.out.println("excel文件转换后pdf异常-htmlfile-"+htmlfile); return false; } finally { app.invoke("Quit", new Variant[] {}); ComThread.Release(); } } }