jacob转换word、excel为html

 
 
package com.fm.tool;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class office2html {
	
	
	// 8 代表word保存成html  
    public static final int WORD_HTML = 8;   
    public static final int EXCEL_HTML = 44;
  
    /**   
     * WORD转HTML   
     * @param docfile WORD文件全路径
     * @param htmlfile 转换后HTML存放路径   
     */
    public static boolean wordToHtml(String docfile, String htmlfile)
    {
    	boolean flag = true;
    	System.out.println("***正在转换WORD***:"+docfile);
    	
    	// 启动word应用程序(Microsoft Office Word 2003) 
    	ActiveXComponent app = new ActiveXComponent("Word.Application");
        
        try
        {     
            // 设置word应用程序不可见    
            app.setProperty("Visible", new Variant(false));    
            // documents表示word程序的所有文档窗口,(word是多文档应用程序)  
            Dispatch docs = app.getProperty("Documents").toDispatch();    
            // 打开要转换的word文件
            Dispatch doc = Dispatch.invoke(
                    docs,
                    "Open",
                    Dispatch.Method,
                    new Object[] { docfile, new Variant(false),   
                            new Variant(true) }, new int[1]).toDispatch();     
            // 作为html格式保存到临时文件  
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {     
                    htmlfile, new Variant(WORD_HTML) }, new int[1]);
            // 关闭word文件  
            Dispatch.call(doc, "Close", new Variant(false));
        }     
        catch (Exception e)     
        {     
        	flag = false;
        	System.out.println("*****转换WORD失败********"); 
            //e.printStackTrace();     
        }     
        finally    
        {     
            //关闭word应用程序  
            app.invoke("Quit", new Variant[] {});     
        }   
        System.out.println("*****转换WORD完毕********");  
        return flag;
    }  
    
    
    public static boolean excelToHtml(String xlsfile, String htmlfile)     
    {     
    	boolean flag = true;
    	System.out.println("***正在转换EXCEL***:"+xlsfile);
    	
    	// 启动excel  
    	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);
        }     
        catch (Exception e)     
        {     
        	flag = false;
        	System.out.println("*****转换EXCEL失败********");
            //e.printStackTrace();
        }     
        finally    
        {     
            app.invoke("Quit", new Variant[] {});     
        }
        System.out.println("*****转换EXCEL完毕********");  
        return flag;
    }     
}
	public void getOffice2HtmlPath(){
		String type = request.getParameter("type");
		String guid = request.getParameter("guid");
		String msg = "err";
		boolean flag = false;
		//建立文件夹
		String dirpath = ServletActionContext.getServletContext().getRealPath("/AffixFiles/OfficeView/");
		File pathDir = new File(dirpath);
		if (!pathDir.exists()){pathDir.mkdirs();}
		
		String filePath = ServletActionContext.getServletContext().getRealPath("/AffixFiles/FileManager/")+"\\"+guid+"."+type;
		String savePath = ServletActionContext.getServletContext().getRealPath("/AffixFiles/OfficeView/")+"\\"+guid+".html";
		String path = request.getContextPath();
		File file = new File(savePath);
		String htmlPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/AffixFiles/OfficeView/"+guid+".html";
		if(file.exists()){
			msg = htmlPath;
		}else{
			if(type.equals("DOC")||type.equals("DOCX")){
				flag = o2h.wordToHtml(filePath, savePath);
			}else if(type.equals("XLS")||type.equals("XLSX")){
				flag = o2h.excelToHtml(filePath, savePath);
			}
			if(flag){msg = htmlPath;}
		}
		tool.writerJson(response, msg);
	}


你可能感兴趣的:(jacob转换word、excel为html)