当前位置: 代码迷 >> HTML/CSS >> java 将word 转化作 Html
  详细解决方案

java 将word 转化作 Html

热度:151   发布时间:2012-10-08 19:54:56.0
java 将word 转化为 Html
//这个例子同样使用了jadoc.jar 可以参考上一篇取到这个jar.
package com.mybook.demo;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordToHtml{
    public boolean changeFormat (String FileName){
        
        String FileFormat = "";
        FileFormat = FileName.substring(FileName.length()-4,FileName.length());
        System.out.println(FileFormat);
        
        if(FileFormat.equalsIgnoreCase(".doc")){
            String DocFile = FileName;
            
            System.out.println("word文件路径:"+DocFile);
            //word文件的完整路径
            
            String HtmlFile = DocFile.substring(0, (DocFile.length() - 4)) + ".htm";
            
            System.out.println("htm文件路径:"+HtmlFile);
            //html文件的完整路径
            
            ActiveXComponent app = new ActiveXComponent("Word.Application");
            //启动word
            
            try
{
                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]);
                //作为htm格式保存文件
                
                Dispatch.call(doc, "Close",new Variant(false));
                //关闭文件
                
            }
            catch (Exception e){
                e.printStackTrace();
            }
            finally {
                app.invoke("Quit", new Variant[] {});
                //退出word程序
            }
            //转化完毕
            return true;
        }
        return false;
    }
    public static void main(String[] args ) {
        WordToHtml d = new WordToHtml();
       // d.changeFormat( args[ 0 ] );
        d.changeFormat("D:\\work\\mybook\\Files\\test.doc");
    }
}
  相关解决方案