java 横向输出_java jacob调用打印,word,excel横向打印

该博客介绍了一种使用Java结合Jacob库来实现Word和Excel文档横向打印的方法。通过创建并设置ActiveXComponent,对Excel和Word的PageSetup属性进行调整,将打印方向设置为横向,然后调用PrintOut方法进行打印。同时,为了避免文件无法删除的问题,关闭文档前保存并关闭操作都在finally块中进行了释放资源的操作。
摘要由CSDN通过智能技术生成

public static booleanprintOfficeFile(File f) {if (f != null &&f.exists()) {

String fileNameString=f.getName();

String postfixString=Utils.getPostfix(fileNameString);if (postfixString.equalsIgnoreCase("xls")|| postfixString.equalsIgnoreCase("xlsx")) {/*** 功能:实现excel打印工作*/ComThread.InitSTA();

ActiveXComponent xl= new ActiveXComponent("Excel.Application");try{//System.out.println("version=" +//xl.getProperty("Version"));//不打开文档

Dispatch.put(xl, "Visible", new Variant(false));

Dispatch workbooks= xl.getProperty("Workbooks")

.toDispatch();//打开文档

Dispatch excel = Dispatch.call(workbooks, "Open",

f.getAbsolutePath()).toDispatch();//横向打印(2013/05/24)//Dispatch currentSheet = Dispatch.get(excel, "ActiveSheet")//.toDispatch();//Dispatch pageSetup = Dispatch//.get(currentSheet, "PageSetup").toDispatch();//Dispatch.put(pageSetup, "Orientation", new Variant(2));//每张表都横向打印2013-10-31

Dispatch sheets = Dispatch.get((Dispatch) excel, "Sheets")

.toDispatch();//获得几个sheet

int count = Dispatch.get(sheets, "Count").getInt();//System.out.println(count);

for (int j = 1; j <=count; j++) {

Dispatch sheet= Dispatch.invoke(sheets, "Item",

Dispatch.Get,new Object[] { newInteger(j) },new int[1]).toDispatch();

Dispatch pageSetup= Dispatch.get(sheet, "PageSetup").toDispatch();

Dispatch.put(pageSetup,"Orientation", new Variant(2));

Dispatch.call(sheet,"PrintOut");

}//开始打印

if (excel != null) {//Dispatch.call(excel, "PrintOut");//增加以下三行代码解决文件无法删除bug

Dispatch.call(excel, "save");

Dispatch.call(excel,"Close" , new Variant(true));

excel=null;

}

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

xl=null;return true;

}catch(Exception e) {

e.printStackTrace();return false;

}finally{//始终释放资源

ComThread.Release();

}

}else if (postfixString.equalsIgnoreCase("doc")|| postfixString.equalsIgnoreCase("docx")) {

ComThread.InitSTA();

ActiveXComponent wd= new ActiveXComponent("Word.Application");try{//不打开文档

Dispatch.put(wd, "Visible", new Variant(false));

Dispatch document= wd.getProperty("Documents")

.toDispatch();//打开文档

Dispatch doc = Dispatch.invoke(document, "Open",

Dispatch.Method,newObject[] { f.getAbsolutePath() },new int[1]).toDispatch();//开始打印

if (doc != null) {

Dispatch.call(doc,"PrintOut");//增加以下三行代码解决文件无法删除bug

Dispatch.call(doc, "save");

Dispatch.call(doc,"Close" , new Variant(true));

doc=null;

}

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

wd=null;return true;

}catch(Exception e) {

e.printStackTrace();return false;

}finally{//始终释放资源

ComThread.Release();

}

}else{return false;

}

}else{return false;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值