excel 转 html java_Java中Office(word/ppt/excel)转换成HTML实现

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.*;public classOfficeToHTML {private final static OfficeToHTML oOfficeToHTML = newOfficeToHTML();public staticOfficeToHTML getInstance() {returnoOfficeToHTML;

}publicOfficeToHTML() {

}publicboolean WordtoHtml(String s, String s1) {

ComThread.InitSTA();

ActiveXComponent activexcomponent= newActiveXComponent("Word.Application");

String s2=s;

String s3=s1;

boolean flag= false;try{

activexcomponent.setProperty("Visible", new Variant(false));

Dispatch dispatch= activexcomponent.getProperty("Documents").toDispatch();

Dispatch dispatch1= Dispatch.invoke(dispatch, "Open", 1,new Object[] { s2, new Variant(false), new Variant(true) },new int[1]).toDispatch();

Dispatch.invoke(dispatch1,"SaveAs", 1, newObject[] { s3,new Variant(8) }, new int[1]);

Variant variant= new Variant(false);

Dispatch.call(dispatch1,"Close", variant);

flag= true;

}catch(Exception exception) {

exception.printStackTrace();

}finally{

activexcomponent.invoke("Quit", new Variant[0]);

ComThread.Release();

ComThread.quitMainSTA();

}returnflag;

}publicboolean PPttoHtml(String s, String s1) {

ComThread.InitSTA();

ActiveXComponent activexcomponent= newActiveXComponent("PowerPoint.Application");

String s2=s;

String s3=s1;

boolean flag= false;try{

Dispatch dispatch= activexcomponent.getProperty("Presentations")

.toDispatch();

Dispatch dispatch1= Dispatch.call(dispatch, "Open", s2,new Variant(-1), new Variant(-1), new Variant(0))

.toDispatch();

Dispatch.call(dispatch1,"SaveAs", s3, new Variant(12));

Variant variant= new Variant(-1);

Dispatch.call(dispatch1,"Close");

flag= true;

}catch(Exception exception) {

System.out.println("|||" +exception.toString());

}finally{

activexcomponent.invoke("Quit", new Variant[0]);

ComThread.Release();

ComThread.quitMainSTA();

}returnflag;

}publicboolean ExceltoHtml(String s, String s1) {

ComThread.InitSTA();

ActiveXComponent activexcomponent= newActiveXComponent("Excel.Application");

String s2=s;

String s3=s1;

boolean flag= false;try{

activexcomponent.setProperty("Visible", new Variant(false));

Dispatch dispatch=activexcomponent.getProperty("Workbooks").toDispatch();

Dispatch dispatch1= Dispatch.invoke(dispatch, "Open", 1, newObject[] {

s2,new Variant(false), new Variant(true)

},new int[1]).toDispatch();

Dispatch.call(dispatch1,"SaveAs", s3, new Variant(44));

Variant variant= new Variant(false);

Dispatch.call(dispatch1,"Close", variant);

flag= true;

}catch(Exception exception)

{

System.out.println("|||" +exception.toString());

}finally{

activexcomponent.invoke("Quit", new Variant[0]);

ComThread.Release();

ComThread.quitMainSTA();

}returnflag;

}public static voidmain(String args[]) {

OfficeToHTML otx=OfficeToHTML.getInstance();

boolean flag1= otx.PPttoHtml("e:/test/test3.pptx", "e:/test/test3.html");if(flag1){

System.out.println("PPT文件转换成HTML成功!");

}else{

System.out.println("PPT文件转换成HTML失败!");

}}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值