I have an excel sheet where all the calculations are done. I want to create a GUI in java where I can input the value needed for calculations to be done in excel sheet. The output (result) along with the chart obtained in the excel sheet should be displayed back in the GUI frame. The excel sheet should run in the background and not be visible to users.
-
1I would suggest to avoid working with MS-Office what so ever. A better solution will be to work with a standard CSV file, for which you have a lot of tools to interact with, or you can build your own tool easily.RoiG– RoiG2012-01-22 06:25:32 +00:00Commented Jan 22, 2012 at 6:25
4 Answers
I'm not sure whether http://www.exceleverywhere.com/java.htm is what you need or not.
By the way Open Office have some remote API for evaluation excel (and other Office) files, Logical Doc uses this feature. may be you can find the solution in its community edition source code.
Comments
I found this, Using COM from Java. You should be able to use the microsoft vitrual machine and microsoft sdk for java 4.0 to use the automation server embedded in Excel ("Excel.Application").
If you don't want to use a the Microsoft Virtual Machine and SDK to compile and run the code, there is also: JACOB: A JAva-COM Bridge, which can be run from any VM.
Taken directly from the above linked JACOB page:
The following example uses Microsoft® Excel as an Automation server:
import com.ms.com.*;
import com.ms.activeX.*;
public class DispatchTest
{
public static void main(String[] args)
{
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
Object xlo = xl.getObject();
try {
System.out.println("version="+xl.getProperty("Version"));
System.out.println("version="+Dispatch.get(xlo, "Version"));
xl.setProperty("Visible", new Variant(true));
Object workbooks = xl.getProperty("Workbooks").toDispatch();
Object workbook = Dispatch.get(workbooks,"Add").toDispatch();
Object sheet = Dispatch.get(workbook,"ActiveSheet").toDispatch();
Object a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] {"A1"},
new int[1]).toDispatch();
Object a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] {"A2"},
new int[1]).toDispatch();
Dispatch.put(a1, "Value", "123.456");
Dispatch.put(a2, "Formula", "=A1*2");
System.out.println("a1 from excel:"+Dispatch.get(a1, "Value"));
System.out.println("a2 from excel:"+Dispatch.get(a2, "Value"));
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
xl.invoke("Quit", new Variant[] {});
}
}
}
As it stands, the code will only compile with JVC (Microsoft's compiler) and only run under JVIEW (Microsoft's VM). However, if you have the JACOB distribution installed, then you can replace the two top lines with:
import com.jacob.com.*;
import com.jacob.activeX.*;
and now, you can compile this with any Java compiler and run it using any Java VM on any Win32 platform.
Comments
Google is your friend. Here're some findings: JExcelApi with a nice example here, and Apache POI which also seems pretty powerful