HOWTO: Automate Excel from JavaLast reviewed: January 29, 1998Article ID: Q169796 |
The information in this article applies to:
SUMMARYThis article illustrates how to call a COM object like Excel from Java. It provides code samples that show how you can make an Excel application visible and open an existing Excel file. There are two code samples. One illustrates how to invoke Excel 7.0 from Java and the other shows how to invoke Excel 8.0 from Java. You will find the classes and interfaces to be quite different in Excel 8.0.
MORE INFORMATIONBefore using code samples 1 and 2, follow these steps below:
Code Sample 1
import xl5en32.*; import com.ms.com.*; public void TestExcel() { _Global gbl = (_Global) new _ExcelApplication(); Variant param = gbl.Application(); Application app = (Application) param.getDispatch(); param.putInt(xl5en32.Constants.xlVisible); app.putVisible(param); Variant vtemp = new Variant(); vtemp.noParam(); Variant vtWorkbooks = Dispatch.get(gbl,"Workbooks"); Workbooks wbs = (Workbooks)vtWorkbooks.getDispatch(); Variant vtName = new Variant(); VtName.putStringRef("C:\\book1.xls"); Variant vtEmpty = new Variant(); vtEmpty.noParam(); wbs.Open(vtName,vtEmpty,vtEmpty,vtEmpty,vtEmpty,vtEmpty,vtEmpty, vtEmpty,vtEmpty,vtEmpty,vtEmpty,vtEmpty,vtEmpty); } Code Sample 2
import excel8.*; import com.ms.com.*; _Global globXL=null; _Application appXL=null; Workbooks books=null; _Workbook book = null; try{ globXL = (_Global)new Global(); appXL = (_Application)globXL.getApplication(); appXL.putVisible(0,true); books = (Workbooks)appXL.getWorkbooks(); Variant vTemp = new Variant(); vTemp.putString("c:\\book1.xls"); Variant vOptional = new Variant(); vOptional.noParam(); book =(_Workbook)books.Open("c:\\book1.xls",vOptional,vOptional,vOptional,vOption al,vOptional,vOptional,vOptional,vOptional,vOptional,vOptional,vOptional,vO ptional,0);
} catch(ComFailException e) { System.out.println(e.getMessage()); }The above code will make the Excel application visible, and it will open up an existing Excel file.
REFERENCESFor the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:
http://support.microsoft.com/support/visualj/ http://support.microsoft.com/support/java/ Keywords : kbcode kbinterop kbprg JCOM Technology : kbInetDev Platform : WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |