try { com=new ActiveXComponent("Word.Application"); com.setProperty("Visible", new Variant(false));
// OLE: set the default printer if (uncName!=null && uncName.length()>0) { com.setProperty("ActivePrinter", new Variant(uncName)); writeLog(out, clientID+" Word set active printer to: "+uncName); } Object documents=com.getProperty("Documents").toDispatch(); Dispatch.call(documents,"Open",filename); writeLog(out, clientID+" client's file has been opened: "+filename); // PrinterTrays int lvFrontPageTray=0; int lvDefaultTray=0;
if (frontPageTray!=null && frontPageTray.length()>0) { lvFrontPageTray=Integer.parseInt(frontPageTray); }
if (defaultTray!=null && defaultTray.length()>0) { lvDefaultTray=Integer.parseInt(defaultTray); }
Dispatch.put(PageSetup, "FirstPageTray", new Variant(lvFrontPageTray)); writeLog(out, clientID+" Word set FirstPageTray to: "+lvFrontPageTray);
Dispatch.put(PageSetup, "OtherPagesTray", new Variant(lvDefaultTray)); writeLog(out, clientID+" Word set OtherPagesTray to: "+lvDefaultTray); com.invoke("PrintOut", new Variant[] {new Variant(false)}); writeLog(out, clientID+" client's file has sent to the printer"); Dispatch.call(ActiveDoc, "Save"); writeLog(out, clientID+" client's file has been saved.");
// Quite Microsoft Word com.invoke("Quit", new Variant[] {}); writeLog(out, clientID+" client's word application has requested to quit"); } catch (Throwable e) { writeLog(out, clientID+" client has error in printing the word document: "+e.getMessage()); e.printStackTrace(); return false; }
去下载一个免费的Jacob.jar吧,网上很多,是专门Java-com的桥
用法很简单的,给你一段操作Word的例子吧。
import com.jacob.com.*;
import com.jacob.activeX.*;
ActiveXComponent com;
try {
com=new ActiveXComponent("Word.Application");
com.setProperty("Visible", new Variant(false));
// OLE: set the default printer
if (uncName!=null && uncName.length()>0) {
com.setProperty("ActivePrinter", new Variant(uncName));
writeLog(out, clientID+" Word set active printer to: "+uncName);
} Object documents=com.getProperty("Documents").toDispatch();
Dispatch.call(documents,"Open",filename);
writeLog(out, clientID+" client's file has been opened: "+filename); // PrinterTrays
int lvFrontPageTray=0;
int lvDefaultTray=0;
if (frontPageTray!=null && frontPageTray.length()>0) {
lvFrontPageTray=Integer.parseInt(frontPageTray);
}
if (defaultTray!=null && defaultTray.length()>0) {
lvDefaultTray=Integer.parseInt(defaultTray);
}
Object ActiveDoc = com.getProperty("ActiveDocument").toDispatch();
Object PageSetup = Dispatch.get(ActiveDoc, "PageSetup").toDispatch();
Dispatch.put(PageSetup, "FirstPageTray", new Variant(lvFrontPageTray));
writeLog(out, clientID+" Word set FirstPageTray to: "+lvFrontPageTray);
Dispatch.put(PageSetup, "OtherPagesTray", new Variant(lvDefaultTray));
writeLog(out, clientID+" Word set OtherPagesTray to: "+lvDefaultTray);
com.invoke("PrintOut", new Variant[] {new Variant(false)});
writeLog(out, clientID+" client's file has sent to the printer"); Dispatch.call(ActiveDoc, "Save");
writeLog(out, clientID+" client's file has been saved.");
// Quite Microsoft Word
com.invoke("Quit", new Variant[] {});
writeLog(out, clientID+" client's word application has requested to quit");
} catch (Throwable e) {
writeLog(out, clientID+" client has error in printing the word document: "+e.getMessage());
e.printStackTrace();
return false;
}