Microsoft KB Archive/169796

= How To Automate Excel from Java =

Article ID: 169796

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Java Virtual Machine
 * Microsoft Visual J++ 1.0 Standard Edition
 * Microsoft Visual J++ 1.1 Standard Edition
 * Microsoft Software Development Kit for Java 2.02
 * Microsoft Software Development Kit for Java 3.2
 * Microsoft Software Development Kit for Java 2.01
 * Microsoft Software Development Kit for Java 2.02
 * Microsoft Software Development Kit for Java 3.0
 * Microsoft Software Development Kit for Java 3.1
 * Microsoft Internet Explorer 3.0
 * Microsoft Internet Explorer 3.01
 * Microsoft Internet Explorer 3.02
 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.01 Service Pack 1
 * Microsoft Internet Explorer 4.01 Service Pack 2

-



This article was previously published under Q169796



SUMMARY
This 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 INFORMATION
Before using code samples 1 and 2, follow these steps below:  Use the Java Applet Wizard to create a default Applet or Application. Run JAVATLB or JACTIVEX (utility similar to JAVATLB and supported by the SDK for Java 2.0x) on the Microsoft Excel 7.0 Object Library if you are using Excel 7.0 or the Microsoft Excel 8.0 Object Library if you are using Excel 8.0. This will create Java descriptions of the Excel Object. If you are using Excel 97, run the JACTIVEX tool on EXCEL8.OLB NOTE: If you are using JACTIVEX, then you will need to build this sample with the JVC.EXE that ships with the SDK for Java 2.0 or later. You can download the SDK for Java from the following Microsoft Web site:

http://www.microsoft.com/mscorp/java/

 Use the import statement to import the classes of the Excel Object Library into your Java project. Insert either one of the two code snippets in your Java Project to automate Excel from Java.

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); // in Excel 97 use: // appXL.setVisible(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.

