Microsoft KB Archive/169806

= PRB: ClassCastException Error When Using Excel Object in Java =

Article ID: 169806

Article Last Modified on 8/25/2005

-

APPLIES TO


 * Microsoft Visual J++ 1.0 Standard Edition
 * Microsoft Visual J++ 1.1 Standard Edition

-



This article was previously published under Q169806



SYMPTOMS
Attempting to create a new _ExcelApplicaton object and casting it to an Application interface, generates the following exception:

exception:java.lang.ClassCastException:xl5en32/_ExcelApplication



RESOLUTION
To work around the problem call getDispatch on the Application object, in order to get at its COM interface. For the code mentioned in the MORE INFORMATION section, the workaround is to use a Workspace, and it's method .Application to return an application object, and then use the getDispatch to get the Application interface: import xl5en32.*; import com.ms.com.*; public void TestExcel {  Variant v1 = new Variant; Application app;

// Create A new worksheet Worksheet wks = (Worksheet)new _ExcelSheet;

// Get hold of a dispatch on the application v1 = wks.Application; app = (Application)v1.getDispatch; }



Steps to Reproduce Behavior
 Run JAVATLB on Excel 5.0 Object Library. This creates Java class wrappers of the Excel objects and interfaces in the Type Library.  Create a Java Application that contains the following code: import xl5en32.*;

public void TestExcel {       _ExcelApplication x = new _ExcelApplication; Application xlApp = (Application)x; }                    Build the application and execute it. This causes a ClassCastException error when creating the Excel Automation object.

