It is possible to connect Java and MS Excel using various technologies. Some of theses technologies include COM and DCOM wrappers like Groovy Scriptom, or J-Interop. Or other direct addin solutions like XLLoop. In this article, we want to follow a different paths: Webservices with SOAP. This connection type is designed for the internet with HTTP requests and wide support in Java and the .net world. This way, we can write an Excel Addin in C# and call Java objects from there.
If you want to create a webservice in Java, it it pretty simple. First, we construct the object, which we want to access from C#. Note, that the access from C# will work using the Windows Communication Foundation (WCF), which wraps the SOAP access and make the whole setup easy.
The following example is a Java class, which we want to access from C#:
package com.thetaris.MyWebService; import java.util.HashMap; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style=Style.RPC) public class Calculator { public long addValues(int val1, int val2) { return val1 + val2; } public double[] getList(int numberElements) { double[] res = new double[numberElements]; for (int i = 0; i { res[i] = i*1.2; } return res; } }
Then, we need a Java server, which can publish an object of the class above:
package com.thetaris.MyWebService; import javax.xml.ws.Endpoint; public class MyWebService { public static void main (String args[]) { Calculator server = new Calculator(); Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", server); } }
After starting the Java server, you can test the function using your browser at
“http://localhost:8080/calculator”
and you should be able to see the Webservice and a link to the Webservice Description WSDL.
Now, we can access this Webservice from C#. First, we need to create a new project in Visual Studio. Within this project, we run the SvcUtil command in order to generate the C# Stubs required. This can look like:
"c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\SvcUtil.exe" /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8080/calculator?wsdl
This creates the files “generatedProxy.cs” and “app.config”, which you have to add to your Visual Studio project. Furthermore, add also a reference to “System.ServiceModel” to your Visual Studio project. Then, the following code will access the Webservice:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace WebServiceClientCSharp { class Program { static void Main(string[] args) { CalculatorClient client = new CalculatorClient(); Console.WriteLine("Result of 3 + 7 is " + client.addValues(3, 7)); for (int j = 0; j < 100; j++) { double?[] res = client.getList(10); for (int i = 0; i < res.Length; i++) { Console.WriteLine("Element is " + res[i]); } } Console.WriteLine("Done."); Console.ReadLine(); } } }
Now, we can use NetOffice, VSTO (as Excel macro) or Excel DNA (as Excel UDF) for integrating C# and Excel. Then, we are done. We see that this is actually very simple. Different data types are possible, too. Some data type mappings are given here.
Update 2011-12-14: Instead of calling the “SvcUtil.exe” for generating the C# stubs, you can also use
"c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\wsdl.exe" /language:cs /out:generatedProxy.cs http://localhost:8080/calculator?wsdl
Govert
Another approach is to use the IKVM Java runtime (http://www.ikvm.net/) to export the functions through .NET and Excel-DNA (http://exceldna.codeplex.com). I have not tried it myself, but this post suggests that it works fine – http://groups.google.com/group/exceldna/browse_frm/thread/7763a8d215970a26. IKVM also allows you to reference Java libraries from C# directly.