【问题标题】:Read Excel with Macro from Java从 Java 中读取带有宏的 Excel
【发布时间】:2015-02-24 03:25:05
【问题描述】:

我有优秀的。我为 excel 文件创建宏以从其他资源中读取数据。该宏每秒运行一次并更新其 Excel 单元格。

现在,我想构建一个java程序来每秒读取excel数据。我已经尝试过 Apache POI,但是在我检查了documentation ti 后,ti 不支持使用宏读取 excel 文件。

我从一些资源中读取 Java Com Bridge (JCOB) 可用于读取带有宏的 excel。我试过了,但是每次我尝试我的代码时单元格值仍然没有更新。

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)  
 {  

 }



}

【问题讨论】:

    标签: java excel vba


    【解决方案1】:

    我不熟悉能够执行您描述的操作的 Excel 引擎。

    您是否考虑过在运行电子表格时改为使用 Excel 并询问它的值?我相信您可以使用 ODBC 做到这一点。

    另一种方法可能是创建工作表的 OpenOffice 版本并改为与 OpenOffice 对话。

    【讨论】:

    • 当前系统使用excel从其他资源中读取数据。我的工作是创建程序以通过 Internet 将 Excel 数据发布到某个网站。在工作表中有 2 个表,我认为使用 ODBC 无效,因为它特定于一张表上的一个表。
    • 如果 Excel 需要做这项工作,您需要与 Excel 对话,而不是解析 Excel 数据文件。
    • 与 Excel 对话是什么意思?对不起,我的英语不够好。
    • @adisembiring,仅在文件中有一个宏并不意味着它会在给定框架加载文件时神奇地执行。您需要通过 Excel 执行它,然后使用例如连接到 Excel。 ODBC 询问单元格的值是什么。然后再问,再问,再问。
    【解决方案2】:

    一个陷阱是 poi 不改变现有 excel 写入另一个文件的值,你可以看到区别

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

    并将这个(更改的)工作簿写入另一个文件

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

    【讨论】:

    • 不可能这样。因为宏改变了excel的值。宏从某个数据源读取数据,并更新其单元格
    • 我知道它会在运行时更改它,但除非您将此工作簿写入另一个文件,否则您看不到差异。请测试一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多