Java enables you to print Excel files using Jacob

Source: Internet
Author: User

 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

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.