1 package org.jinterop.dcom.test; 2 3 4 5 import java.net.UnknownHostException ; 6 import java.util.Date ; 7 8 import org.jinterop.dcom.common.JIException; 9 import org.jinterop.dcom.core.IJIComObject; 10 import org.jinterop.dcom.core.JIArray; 11 import org.jinterop.dcom.core.JIComServer; 12 import org.jinterop.dcom.core.JIInterfacePointer; 13 import org.jinterop.dcom.core.JIProgId; 14 import org.jinterop.dcom.core.JISession; 15 import org.jinterop.dcom.core.JIString; 16 import org.jinterop.dcom.core.JIVariant; 17 import org.jinterop.dcom.win32.ComFactory; 18 import org.jinterop.dcom.win32.IJIDispatch; 19 20 public class MSExcel2 { 21 22 private JIComServer comServer = null; 23 private IJIDispatch dispatch = null; 24 private IJIComObject unknown = null; 25 private IJIDispatch dispatchOfWorkSheets = null; 26 private IJIDispatch dispatchOfWorkBook = null; 27 private IJIDispatch dispatchOfWorkSheet = null; 28 private JISession session = null; 29 public MSExcel2(String address, String [] args) throws JIException, UnknownHostException 30 { 31 session = JISession.createSession(args[1],args[2],args[3]); 32 comServer = new JIComServer(JIProgId.valueOf(session,"Excel.Application"),address,session); 33 } 34 35 public void startExcel() throws JIException 36 { 37 unknown = comServer.createInstance(); 38 dispatch = (IJIDispatch)ComFactory.createCOMInstance(ComFactory.IID_IDispatch,unknown); 39 } 40 41 public void showExcel() throws JIException 42 { 43 int dispId = dispatch.getIDsOfNames("Visible"); 44 JIVariant variant = new JIVariant(Boolean.TRUE); 45 dispatch.put(dispId,variant); 46 } 47 48 public void createWorkSheet() throws JIException 49 { 50 int dispId = dispatch.getIDsOfNames("Workbooks"); 51 52 JIVariant outVal = dispatch.get(dispId); 53 JIInterfacePointer ptr = (JIInterfacePointer)outVal.getObject(); 54 IJIDispatch dispatchOfWorkBooks =(IJIDispatch)ComFactory.createCOMInstance(unknown,ptr); 55 56 57 JIVariant[] outVal2 = dispatchOfWorkBooks.callMethodA("Add",new Object []{JIVariant.OPTIONAL_PARAM}); 58 ptr = (JIInterfacePointer)outVal2[0].getObject(); 59 dispatchOfWorkBook =(IJIDispatch)ComFactory.createCOMInstance(unknown,ptr); 60 61 outVal = dispatchOfWorkBook.get("Worksheets"); 62 63 ptr = (JIInterfacePointer)outVal.getObjectAsInterfacePointer(); 64 dispatchOfWorkSheets =(IJIDispatch)ComFactory.createCOMInstance(unknown,ptr); 65 66 outVal2 = dispatchOfWorkSheets.callMethodA("Add",new Object []{JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM}); 67 ptr = (JIInterfacePointer)outVal2[0].getObject(); 68 dispatchOfWorkSheet =(IJIDispatch)ComFactory.createCOMInstance(unknown,ptr); 69 } 70 71 public void pasteArrayToWorkSheet() throws JIException 72 { 73 int dispId = dispatchOfWorkSheet.getIDsOfNames("Range"); 74 JIVariant variant = new JIVariant(new JIString("A1:C3")); 75 Object [] out = new Object []{JIVariant.class}; 76 JIVariant[] outVal2 = dispatchOfWorkSheet.get(dispId, new Object []{variant}); 77 JIInterfacePointer ptr = (JIInterfacePointer)outVal2[0].getObject(); 78 IJIDispatch dispRange = (IJIDispatch)ComFactory.createCOMInstance(unknown,ptr); 79 80 81 Object [][] newValue = { 82 { new JIString("defe"), new Boolean (false), new Double (98765.0 / 12345.0)}, 83 { new Date (), new Integer (5454), new Float (22.0 / 7.0) }, 84 { new Boolean (true), new JIString("dffe"), new Date () } 85 }; 86 87 89 dispRange.put("Value2", new JIVariant(new JIArray(newValue))); 90 91 try { 92 Thread.sleep(10000); 93 } catch (InterruptedException e) { 94 e.printStackTrace(); 95 } 96 97 JIVariant variant2 = dispRange.get("Value2"); 98 JIArray newValue2 = variant2.getObjectAsArray(); 99 newValue = (Object [][])newValue2.getArrayInstance(); 100 for(int i = 0; i < newValue.length; i++){ 101 for(int j = 0; j < newValue[i].length; j++){ 102 System.out.print(newValue[i][j] + "\t"); 103 } 104 System.out.println(); 105 } 106 107 dispatchOfWorkBook.callMethod("close",new Object []{Boolean.FALSE,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM}); 108 dispatch.callMethod("Quit"); 109 JISession.destroySession(session); 110 } 111 112 113 public static void main(String [] args) { 114 115 try { 116 if (args.length < 4) 117 { 118 System.out.println("Please provide address domain username password"); 119 return; 120 } 121 MSExcel2 test = new MSExcel2(args[0],args); 122 test.startExcel(); 123 test.showExcel(); 124 test.createWorkSheet(); 125 test.pasteArrayToWorkSheet(); 126 } catch (Exception e) { 127 e.printStackTrace(); 129 } 130 } 131 132 133 134 135 136 } 137 | Popular Tags |