Adi Sembiring
Adi Sembiring

Reputation: 5936

Read Excel with Macro from Java

I have excel. and I create macro to the excel file to read data from other resources. the macro run every second and update its excel cells.

Now, I want to build java program to read the excel data every seconds to. I have try Apache POI, but after I check the documentation ti doesn't support reading excel file with macro.

I read from some resources Java Com Bridge (JCOB) can be used to read excel with macro. I've try, But the cell value still not updated every times I try my code.

import com.jacob.com.*;
import com.jacob.activeX.*;

public class ExcelTest {
 private static ActiveXComponent xl;
 private static Dispatch workbooks = null;
 private static Dispatch workbook = null;
 private static Dispatch sheet = null;
 private static String filename = null;
 private static boolean readonly = false;

 public static void main(String[] args) {
  String file = "D:\\tutorial\\ApachePoi\\ratesource.xls";
  OpenExcel(file, false); // do not show false to open Excel
  System.out.println(GetValue("B46"));
 }

 private static void OpenExcel(String file, boolean f) {
  try {
   filename = file;
   xl = new ActiveXComponent("Excel.Application");
   xl.setProperty("Visible", new Variant(f));
   workbooks = xl.getProperty("Workbooks").toDispatch();
   workbook = Dispatch.invoke(
     workbooks,
     "Open",
     Dispatch.Method,
     new Object[] { filename, new Variant(false),
       new Variant(readonly) },// whether to open read-only
     new int[1]).toDispatch();
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 // Read value
 private static String GetValue(String position) {
  if (workbook == null) {
   System.out.println("workbook is null");
  }
  sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
  Object cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
    new Object[]{position}, new int[1]).toDispatch();
  String value = Dispatch.get((Dispatch) cell, "Value").toString();

  return value;
 }
 //1.3638356164383563
 //1.3638356164383563




 private static void SetValue (String position, String type, String value)  
 {  

 }



}

Upvotes: 0

Views: 7657

Answers (2)

user467871
user467871

Reputation:

One pitfall is poi don't change the value of the existing excel write to another file and you can see the difference

workbook.getSheet().getRow(1).getCell(0).setValue("test");

and write this(changed) workbook to another file

public void writeFile(String fileName) {
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(fileName);
        getWorkbook().write(fos);
        fos.close();
    } catch (IOException e) {
        System.out.println("IOException occurs");
        e.printStackTrace();
    }
}

Upvotes: 0

I am unfamiliar with an Excel engine capable of doing what you describe.

Have you considered talking to Excel instead and ask it for its values when running your spread-sheet? I believe you can do so with ODBC.

Another approach might be creating an OpenOffice version of your sheet and talk to OpenOffice instead.

Upvotes: 1

Related Questions