/** * for printing * * @param file * @param saveFile * @param preFix * @param formatfile * @return * @throws noofficeexception * * public static void Doprintdoc (file file) {if (file = = NULL | |!file.exists ()) {return;} Gets the file suffix name int index = File.getname (). IndexOf ("."); String extname = File.getname (). toUpperCase (). substring (index + 1); String Comapp = "Word.Application"; String property = "Documents";//if it is word if (extname.equals ("DOC") | | | extname.equals ("DOCX") | | extname.equals ("WPS")) {Comapp = "Word.Application";p roperty = "Documents";} else if (extname.equals ("XLS") | | | extname.equals ("XLSX") | | extname.equals ("ET")) {Comapp = "Excel.Application"; property = "Workbooks";} else if (extname.equals ("PPT") | | | extname.equals ("PPTX") | | extname.equals ("DPS")) {Comapp = "powerpoint.application"; property = "Presentations";} Initialize the component Comthread.initsta ();//define the Component Object Activexcomponent AXC = new Activexcomponent (Comapp); try {if (!property.equals (" Presentations ")) {Dispatch.put (AXC," Visible ", new Variant (false));} Dispatch document = Axc.getproperty (property). Todispatch ();D Ispatch doc = null;//if it is ppt, if (Property.equals (" Presentations ")) {doc = Dispatch.call (document," Open ", File.getabsolutepath (), True, True, false). Todispatch ();} else { doc = Dispatch.invoke (document, "Open", Dispatch.method, new object[] {File.getabsolutepath ()}, new Int[1]). Todispatch ( );} Dispatch.call (Doc, "PrintOut");D Ispatch.call (Doc, "Close"), if (!property.equals ("Presentations")) {Axc.invoke (" Quit ", new variant[] {});}} catch (Exception e) {e.printstacktrace ();} finally {Comapp = "";p roperty = ""; Comthread.release ();}}
Required jar and DLL download path http://download.csdn.net/detail/daixinmei/7962251
DLL to be placed under Windows/system32, or under the bin of the JDK installation directory
Java calls printer to print files