java office 轉 html

java office 轉 html
用 jacob jar 進行轉換
最新 版本可到官方下   http://sourceforge.net/projects/jacob-project/
裏面有 jacob.jar  和 相應的 jacob-1.15-M4-x86.dll
jar包 放到項目中  dll 分別放到 java 的jdk/bin和 jdk/jre/lib/ext和C:\WINDOWS\system32 目錄下  保持版本一致
 
一下爲實現代碼。

package com.dsit.pysf.util;

/**
 * 〈p〉Title:Word文檔轉html類〈/p〉
 * 〈p〉Description: 〈/p〉
 * 〈p〉Copyright:() 2011〈/p〉
 * @author Sarsea
 * @version 1.0
 */
import java.io.File;

import com.jacob.com.*;
import com.jacob.activeX.*;

public class OfficetoHtml {
 /**
  * word文檔轉換html函數
  *
  * @param docfile
  *            word文檔的絕對路徑加文件名(包含擴展名)
  * @param htmlfile
  *            轉換後的html文件絕對路徑和文件名(不含擴展名)
  */
 public static void wordTohtml(String docfile, String htmlfile) {
  System.out.println("begin :"+docfile);
  if (docfile.substring(docfile.lastIndexOf('.') + 1).indexOf("doc") < 0) {
   System.out.println("OfficetoHtml.wordTohtml : 不是word文檔");
   return;
  }
  ActiveXComponent app = null;
  try {
   app = new ActiveXComponent("Word.Application"); // 啓動word
   app.setProperty("Visible", new Variant(false));
   // 設置word不可見
   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();
   // 打開word文件
   Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
     htmlfile, new Variant(8) }, new int[1]);
   // 作爲html格式保存到臨時文件
   Variant f = new Variant(false);
   Dispatch.call(doc, "Close", f);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (app != null)
    app.invoke("Quit", new Variant[] {});
  }
 }

 /**
  * word文檔轉換txt函數
  *
  * @param docfile
  *            word文檔的絕對路徑加文件名(包含擴展名)
  * @param txtfile
  *            轉換後的html文件絕對路徑和文件名(不含擴展名)
  */
 public static void wordTotxt(String docfile, String txtfile) {
  if (docfile.substring(docfile.lastIndexOf('.') + 1).indexOf("doc") < 0) {
   System.out.println("OfficetoHtml.wordTotxt : 不是word文檔");
   return;
  }
  ActiveXComponent app = null;
  try {
   app = new ActiveXComponent("Word.Application"); // 啓動word
   app.setProperty("Visible", new Variant(false));
   // 設置word不可見
   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();
   // 打開word文件
   Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
     txtfile, new Variant(7) }, new int[1]);
   // 作爲html格式保存到臨時文件
   Variant f = new Variant(false);
   Dispatch.call(doc, "Close", f);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (app != null)
    app.invoke("Quit", new Variant[] {});
  }
 }

 /**
  * excel文檔轉換函數
  *
  * @param xlsfile
  *            excel文檔的絕對路徑加文件名(包含擴展名)
  * @param htmlfile
  *            轉換後的html文件絕對路徑和文件名(不含擴展名)
  */
 public static void excelTohtml(String xlsfile, String htmlfile) {
  if (xlsfile.substring(xlsfile.lastIndexOf('.') + 1).indexOf("xls") < 0) {
   System.out.println("OfficetoHtml.excelTohtml : 不是excel文檔");
   return;
  }
  ActiveXComponent app = null;
  try {
   app = new ActiveXComponent("Excel.Application"); // 啓動excel
   app.setProperty("Visible", new Variant(false));
   // 設置word不可見
   Dispatch docs = app.getProperty("Workbooks").toDispatch();
   Dispatch doc = Dispatch.invoke(
     docs,
     "Open",
     Dispatch.Method,
     new Object[] { xlsfile, new Variant(false),
       new Variant(true) }, new int[1]).toDispatch();
   // 打開word文件
   Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
     htmlfile, new Variant(44) }, new int[1]);
   // 作爲html格式保存到臨時文件
   Variant f = new Variant(false);
   Dispatch.call(doc, "Close", f);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (app != null)
    app.invoke("Quit", new Variant[] {});
  }
 }

 public static void main(String[] strs) {
  OfficetoHtml
    .wordTohtml(
      "d:/a.docx",
      "d:/a.docx.html");
  // File f=new File("C:\\Program Files\\Apache Software
  // Foundation\\Tomcat 5.5\\webapps\\gzpysf\\a.htm");
  // f.delete();
  

 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章