public static Boolean printofficefile (File f) {if (f! = null && f.exists ()) {String filenamestring = F.getnam E (); String postfixstring = Utils.getpostfix (filenamestring); if (Postfixstring.equalsignorecase ("xls") | | | postfixstring.equalsignorecase ("xlsx")) {/** * function: Achieve Excel print work */Comthread.initsta (); Activexcomponent XL = new Activexcomponent ("Excel.Application"); try {//System.out.println ("version=" +//Xl.getproperty ("version")); Do not open document Dispatch.put (XL, "Visible", new Variant (false)); Dispatch workbooks = Xl.getproperty ("workbooks"). Todispatch (); Open the document Dispatch Excel = Dispatch.call (workbooks, "Open", F.getabsolutepath ()). Todispatch (); Landscape printing (2013/05/24)//Dispatch CurrentSheet = Dispatch.get (Excel, "ActiveSheet")//. Todispatch ();//Dispatch PageSetup = dispatch//. Get (CurrentSheet, "PageSetup"). Todispatch ();//Dispatch.put (PageSetup, "Orientation", n EW Variant (2)); Each table is printed horizontally 2013-10-31 Dispatch sheets = dispatch.get ((Dispatch) Excel, "Sheets"). Todispatch (); Get several 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 [] {new Integer (j)}, new Int[1]). Todispatch (); Dispatch pageSetup = dispatch.get (sheet, "PageSetup"). Todispatch (); Dispatch.put (PageSetup, "Orientation", New Variant (2)); Dispatch.call (sheet, "PrintOut"); }//Begins printing if (excel! = null) {//dispatch.call (Excel, "PrintOut"); Add the following three lines of code to resolve the file cannot delete bug Dispatch.call (Excel, "save"); Dispatch.call (Excel, "Close", New Variant (true)); Excel=null; } xl.invoke ("Quit", new variant[] {}); Xl=null; return true; } catch (Exception e) {e.printstacktrace (); return false; } finally {//Always frees Resource comthread.release (); }} else if (Postfixstring.equalsignorecase ("Doc") | | | postfixstring.equalsignorecase ("docx")) {Comthread.initsta (); Activexcomponent WD = new Activexcomponent ("Word.Application"); try {//Do not open document Dispatch.put (WD, "Visible", new Variant (false)); Dispatch document = Wd.getproperty ("Documents"). Todispatch (); Open Document Dispatch doc = Dispatch.invoke (document, "Open", Dispatch.method, new object[] {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 bug Dispatch.call (Doc, "save"); Dispatch.call (Doc, "Close", New Variant (true)); Doc=null; } wd.invoke ("Quit", new variant[] {}); Wd=null; return true; } catch (Exception e) {e.printstacktrace (); return false; } finally {//Always frees Resource comthread.release (); }} else {return false; }} else {return false; } }
Java Jacob calls print, Word,excel landscape printing