使用本地office安裝包進行檔案轉換操作 ,word\excel\ppt轉換為html
一、把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; }
五、三個附件: