java office 转 html

来源:互联网 发布:linux 写脚本文件 编辑:程序博客网 时间:2024/06/03 10:33
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();
  

 }
}

原创粉丝点击