jacob转html

package com.acdm.util;

importjava.io.BufferedReader;

importjava.io.BufferedWriter;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileWriter;

importjava.io.IOException;

import java.io.InputStreamReader;

import com.jacob.activeX.ActiveXComponent;

importcom.jacob.com.Dispatch;

import com.jacob.com.Variant;

public class TransformFiletoHtml

{

intWORD_HTML=8;

intWORD_TXT=7;

intEXCEL_HTML=44;

/**

*WORD转HTML

*@paramdocfileWORD文件全路径

*@paramhtmlfile转换后HTML存放路径

*/

publicvoidwordToHtml(Stringdocfile,Stringhtmlfile)

{

ActiveXComponentapp=newActiveXComponent("Word.Application");//启动word

try

{

app.setProperty("Visible",newVariant(false));//设置word不可见

Dispatchdocs=app.getProperty("Documents").toDispatch();

//打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,因为我们要保存原文件,所以以可写方式打开。

Dispatchdoc=Dispatch.invoke(docs,"Open",Dispatch.Method,newObject[]{docfile,newVariant(false),newVariant(true)},newint[1]).toDispatch();

//Dispatchselection=app.getProperty("Selection").toDispatch();//获得对Selection组件

//Dispatch.call(selection,"HomeKey",newVariant(6));//移到开头

//Dispatchfind=Dispatch.call(selection,"Find").toDispatch();//获得Find组件

//Dispatch.put(find,"Text","3G");//查找字符串"name"

//Dispatch.call(find,"Execute");//执行查询

//Dispatch.put(selection,"Text","下载");//替换为"张三"

Dispatch.invoke(doc,"SaveAs",Dispatch.Method,newObject[]{htmlfile,newVariant(WORD_HTML)},newint[1]);

Variantf=newVariant(false);

Dispatch.call(doc,"Close",f);

}

catch(Exceptione)

{

e.printStackTrace();

}

finally

{

app.invoke("Quit",newVariant[]{});

}

}

/**

*EXCEL转HTML

*@paramxlsfileEXCEL文件全路径

*@paramhtmlfile转换后HTML存放路径

*/

publicvoidexcelToHtml(Stringxlsfile,Stringhtmlfile)

{

ActiveXComponentapp=newActiveXComponent("Excel.Application");//启动excel

try

{

app.setProperty("Visible",newVariant(false));

Dispatchexcels=app.getProperty("Workbooks").toDispatch();

Dispatchexcel=Dispatch.invoke(excels,"Open",Dispatch.Method,newObject[]{xlsfile,newVariant(false),newVariant(true)},newint[1]).toDispatch();

Dispatch.invoke(excel,"SaveAs",Dispatch.Method,newObject[]{htmlfile,newVariant(EXCEL_HTML)},newint[1]);

Variantf=newVariant(false);

Dispatch.call(excel,"Close",f);

}

catch(Exceptione)

{

e.printStackTrace();

}

finally

{

app.invoke("Quit",newVariant[]{});

}

}

/**

*/删除指定文件夹

*@paramfolderPath文件夹全路径

*@paramhtmlfile转换后HTML存放路径

*/

publicvoiddelFolder(StringfolderPath)

{

try

{

delAllFile(folderPath);//删除完里面所有内容

StringfilePath=folderPath;

filePath=filePath.toString();

java.io.FilemyFilePath=newjava.io.File(filePath);

myFilePath.delete();//删除空文件夹

}catch(Exceptione){e.printStackTrace();}

}

/**

*/删除指定文件夹下所有文件

*@parampath文件全路径

*/

publicbooleandelAllFile(Stringpath)

{

booleanflag=false;

Filefile=newFile(path);

if(!file.exists())

{

returnflag;

}

if(!file.isDirectory())

{

returnflag;

}

String[]tempList=file.list();

Filetemp=null;

for(inti=0;i<tempList.length;i++)

{

if(path.endsWith(File.separator))

{

temp=newFile(path+tempList[i]);

}

else

{

temp=newFile(path+File.separator+tempList[i]);

}

if(temp.isFile())

{

temp.delete();

}

if(temp.isDirectory())

{

delAllFile(path+"/"+tempList[i]);//先删除文件夹里面的文件

delFolder(path+"/"+tempList[i]);//再删除空文件夹

flag=true;

}

}

returnflag;

}

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

TransformFiletoHtmltrans=newTransformFiletoHtml();

trans.wordToHtml("F:\\3G系统体验平台整合方案(整合后台).doc","D:\\sinye.html");

}

}