简体   繁体   English

从Java读取带有宏的Excel

[英]Read Excel with Macro from Java

I have excel. 我擅长。 and I create macro to the excel file to read data from other resources. 然后创建到excel文件的宏,以从其他资源读取数据。 the macro run every second and update its excel cells. 宏每秒运行一次并更新其excel单元格。

Now, I want to build java program to read the excel data every seconds to. 现在,我想构建一个Java程序以每秒读取一次Excel数据。 I have try Apache POI, but after I check the documentation ti doesn't support reading excel file with macro. 我已经尝试过Apache POI,但是在检查了文档后, ti不支持读取带有宏的excel文件。

I read from some resources Java Com Bridge ( JCOB ) can be used to read excel with macro. 我从一些资源中读到了Java Com Bridge( JCOB )可以用来读取带有宏的excel。 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)  
 {  

 }



}

I am unfamiliar with an Excel engine capable of doing what you describe. 我不熟悉能够执行您描述的功能的Excel引擎。

Have you considered talking to Excel instead and ask it for its values when running your spread-sheet? 您是否考虑过与Excel对话,并在运行电子表格时询问它的值? I believe you can do so with ODBC. 我相信您可以使用ODBC来实现。

Another approach might be creating an OpenOffice version of your sheet and talk to OpenOffice instead. 另一种方法可能是创建工作表的OpenOffice版本,然后与OpenOffice联系。

One pitfall is poi don't change the value of the existing excel write to another file and you can see the difference 一个陷阱是poi不要更改现有excel写入另一个文件的值,您可以看到其中的区别

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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