Packagecom. Heibeiedu.test2;ImportJava.io.File;Importcom.jacob.activeX.ActiveXComponent;ImportCom.jacob.com.ComThread;ImportCom.jacob.com.Dispatch;Importcom.jacob.com.Variant; Public classPrintdemo { 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")) { /*** Function: Achieve Excel print work*/Comthread.initsta (); Activexcomponent XL=NewActivexcomponent ("Excel.Application"); Try { //System.out.println ("version=" +//xl.getproperty ("Version")); //do not open documentsDispatch.put (XL, "Visible",NewVariant (false)); Dispatch Workbooks= Xl.getproperty ("Workbooks"). Todispatch (); //Open DocumentDispatch Excel = Dispatch.call (workbooks, "Open", F.getabsolutepath ()). Todispatch (); //Transverse printing (2013/05/24)//Dispatch currentsheet = Dispatch.get (Excel,//"ActiveSheet")//. Todispatch (); //Dispatch pageSetup = Dispatch//. Get (CurrentSheet, "PageSetup"). Todispatch (); //dispatch.put (PageSetup, "Orientation", New Variant (2)); //each table is printed horizontally 2013-10-31Dispatch sheets = dispatch.get ((Dispatch) Excel, "sheets"). Todispatch (); //get a few sheet intCount = Dispatch.get (sheets, "Count"). GetInt (); //System.out.println (count); for(intj = 1; J <= Count; J + +) {Dispatch sheet=Dispatch. Invoke (Sheets,"Item", Dispatch.get,NewObject[] {NewInteger (j)},New int[1]). Todispatch (); Dispatch PageSetup= Dispatch.get (Sheet, "PageSetup"). Todispatch (); Dispatch.put (PageSetup,"Orientation",NewVariant (2)); Dispatch.call (Sheet,"PrintOut"); } //Start printing if(Excel! =NULL) { //Dispatch.call (Excel, "PrintOut"); //Add the following three lines of code to resolve the file cannot delete the bugDispatch.call (Excel, "save"); Dispatch.call (Excel,"Close",NewVariant (true)); Excel=NULL; } xl.invoke ("Quit",Newvariant[] {}); XL=NULL; return true; } Catch(Exception e) {e.printstacktrace (); return false; } finally { //Always release Resourcescomthread.release (); } } Else if(Postfixstring.equalsignorecase ("Doc") | | | postfixstring.equalsignorecase ("docx") ) {Comthread.initsta (); Activexcomponent WD=NewActivexcomponent ("Word.Application"); Try { //do not open documentsDispatch.put (WD, "Visible",NewVariant (false)); Dispatch Document= Wd.getproperty ("Documents")). Todispatch (); //Open DocumentDispatch doc =Dispatch. Invoke (document,"Open", Dispatch.method,NewObject[] {F.getabsolutepath ()},New int[1]). Todispatch (); //Start printing if(Doc! =NULL) {Dispatch.call (doc,"PrintOut"); //Add the following three lines of code to resolve the file cannot delete the bugDispatch.call (Doc, "Save"); Dispatch.call (Doc,"Close",NewVariant (true)); Doc=NULL; } wd.invoke ("Quit",Newvariant[] {}); WD=NULL; return true; } Catch(Exception e) {e.printstacktrace (); return false; } finally { //Always release Resourcescomthread.release (); } } Else { return false; } } Else { return false; } } Public Static voidMain (string[] args) {Printdemo.printofficefile (NewFile ("Hehe.xls")); }}
Java enables you to print Excel files using Jacob