Java Jacob calls print, Word,excel landscape printing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.