简体   繁体   中英

Reading excel file using Java

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.

如果您需要Microsoft Excel集成, JExcel可能对您有所帮助。

如果必须使用Excel进行计算,则可能应将VBA用于GUI。

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.

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.

Google is your friend. Here're some findings: JExcelApi with a nice example here , and Apache POI which also seems pretty powerful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM