1 package org.jinterop.dcom.test; 2 3 4 5 import java.net.UnknownHostException ; 6 7 import org.jinterop.dcom.common.JIException; 8 import org.jinterop.dcom.core.IJIComObject; 9 import org.jinterop.dcom.core.JIArray; 10 import org.jinterop.dcom.core.JIComServer; 11 import org.jinterop.dcom.core.JIInterfacePointer; 12 import org.jinterop.dcom.core.JIProgId; 13 import org.jinterop.dcom.core.JISession; 14 import org.jinterop.dcom.core.JIString; 15 import org.jinterop.dcom.core.JIVariant; 16 import org.jinterop.dcom.win32.ComFactory; 17 import org.jinterop.dcom.win32.IJIDispatch; 18 19 20 public class MSExcel3 { 21 22 23 private JIComServer comServer = null; 24 private IJIDispatch dispatch = null; 25 private IJIComObject unknown = null; 26 private IJIDispatch dispatchOfWorkSheets = null; 27 private IJIDispatch dispatchOfWorkBook = null; 28 29 public MSExcel3(String address, String args[]) throws JIException, UnknownHostException 30 { 31 JISession 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 dispatch.put("DisplayAlerts",new JIVariant(Boolean.TRUE)); 48 } 49 50 public void createWorkSheet() throws JIException 51 { 52 int dispId = dispatch.getIDsOfNames("Workbooks"); 53 54 JIVariant outVal = dispatch.get(dispId); 55 JIInterfacePointer ptr = (JIInterfacePointer)outVal.getObject(); 56 IJIDispatch dispatchOfWorkBooks =(IJIDispatch)ComFactory.createCOMInstance(unknown,ptr); 57 58 59 JIVariant[] outVal2 = dispatchOfWorkBooks.callMethodA("Open",new Object []{ 60 new JIString("C:\\temp\\chart.xls"),Boolean.TRUE,Boolean.TRUE,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM 61 ,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM 62 ,JIVariant.OPTIONAL_PARAM}); 63 ptr = (JIInterfacePointer)outVal2[0].getObject(); 64 dispatchOfWorkBook =(IJIDispatch)ComFactory.createCOMInstance(unknown,ptr); 65 66 outVal = dispatchOfWorkBook.get("Worksheets"); 67 68 ptr = (JIInterfacePointer)outVal.getObjectAsInterfacePointer(); 69 dispatchOfWorkSheets =(IJIDispatch)ComFactory.createCOMInstance(unknown,ptr); 70 71 outVal2 = dispatchOfWorkSheets.get("Item", new Object []{new JIVariant(1)}); 72 IJIDispatch sheet =(IJIDispatch)ComFactory.createCOMInstance(unknown,outVal2[0].getObjectAsInterfacePointer()); 73 outVal2 = sheet.get("Range",new Object []{new JIString("A1:B19"),JIVariant.OPTIONAL_PARAM}); 74 IJIDispatch range =(IJIDispatch)ComFactory.createCOMInstance(unknown,outVal2[0].getObjectAsInterfacePointer()); 75 76 Object [][] newValue = { 77 { new Integer (121), new Integer (117) }, 78 { new Integer (111), new Integer (156) }, 79 { new Integer (132), new Integer (138) }, 80 { new Integer (116), new Integer (119) }, 81 { new Integer (148), new Integer (126) }, 82 { new Integer (163), new Integer (143) }, 83 { new Integer (174), new Integer (135) }, 84 { new Integer (136), new Integer (142) }, 85 { new Integer (142), new Integer (163) }, 86 { new Integer (121), new Integer (117) }, 87 { new Integer (111), new Integer (156) }, 88 { new Integer (132), new Integer (138) }, 89 { new Integer (116), new Integer (119) }, 90 { new Integer (148), new Integer (126) }, 91 { new Integer (163), new Integer (143) }, 92 { new Integer (174), new Integer (135) }, 93 { new Integer (136), new Integer (142) }, 94 { new Integer (142), new Integer (163) }, 95 { new Integer (121), new Integer (117) } 96 }; 97 98 range.put("Value", new JIVariant(new JIArray(newValue))); 99 100 try { 101 Thread.sleep(5000); 102 } catch (InterruptedException e) { 103 e.printStackTrace(); 104 } 105 106 for (int j=0; j<60; j++) { 107 try { 108 Thread.sleep(300); 109 } catch (InterruptedException e) { 110 e.printStackTrace(); 111 } 112 Object temp1 = newValue[0][0]; 113 Object temp2 = newValue[0][1]; 114 int i = 0; 115 for (i=1; i<newValue.length; i++) { 116 for (int k=0; k<newValue[i-1].length; k++) { 117 newValue[i-1][k] = newValue[i][k]; 118 } 119 } 120 121 newValue[i-1][0] = temp1; 122 newValue[i-1][1] = temp2; 123 range.put("Value", new JIVariant(new JIArray(newValue))); 125 } 126 127 outVal2 = sheet.get("ChartObjects",new Object []{JIVariant.OPTIONAL_PARAM}); 128 IJIDispatch chartObjects = (IJIDispatch)ComFactory.createCOMInstance(unknown,outVal2[0].getObjectAsInterfacePointer()); 129 outVal2 = chartObjects.callMethodA("Add",new Object []{new Double (100),new Double (30),new Double (400),new Double (250)}); 130 IJIDispatch chartObject = (IJIDispatch)ComFactory.createCOMInstance(unknown,outVal2[0].getObjectAsInterfacePointer()); 132 outVal = chartObject.get("Chart"); 133 IJIDispatch chart = (IJIDispatch)ComFactory.createCOMInstance(unknown,outVal.getObjectAsInterfacePointer()); 134 chart.callMethod("SetSourceData",new Object []{range,JIVariant.OPTIONAL_PARAM}); 135 try { 136 Thread.sleep(5000); 137 } catch (InterruptedException e) { 138 e.printStackTrace(); 139 } 140 141 outVal = sheet.get("PageSetup"); 142 IJIDispatch pageSetup = (IJIDispatch)ComFactory.createCOMInstance(unknown,outVal.getObjectAsInterfacePointer()); 143 pageSetup.put("Orientation",new JIVariant(2)); 144 pageSetup.put("Zoom",new JIVariant(100)); 145 sheet.callMethod("PrintOut",new Object []{JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM, 146 JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM}); 147 dispatchOfWorkBook.callMethod("close",new Object []{Boolean.FALSE,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM}); 148 dispatch.callMethod("Quit"); 149 JISession.destroySession(dispatch.getAssociatedSession()); 150 } 151 152 153 154 155 public static void main(String [] args) { 156 157 try { 158 if (args.length < 4) 159 { 160 System.out.println("Please provide address domain username password"); 161 return; 162 } 163 MSExcel3 test = new MSExcel3(args[0],args); 164 test.startExcel(); 165 test.showExcel(); 166 test.createWorkSheet(); 167 } catch (Exception e) { 168 e.printStackTrace(); 170 } 171 } 172 173 174 175 176 177 } 178 | Popular Tags |