问题使用jDDE将Java连接到Excel

时间:2012-04-24 17:27:00

标签: java excel dde

我尝试编写示例代码 - 最简单的,我收到连接错误。

package my.excel.dde;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import com.google.code.jdde.client.ClientConversation;
import com.google.code.jdde.client.DdeClient;

public class ExcelDdeExperiment {

    public static void main(String[] args) {
        final String filepath = "C:\\temp\\workbook1.xls";
        final String sheetname = "Sheet1";
        final DdeClient ddeclient = new DdeClient();
        ClientConversation conversation = null;
        String filename = null;

        try {
            Desktop desktop = Desktop.getDesktop();
            File file = new File(filepath);
            desktop.edit(file);
            filename = file.getName();

            conversation = ddeclient.connect(filename, sheetname);
            if (conversation != null) {
                System.out.println("successfully connected");
            }
        } catch (IOException e) {
            throw new RuntimeException("cannot open excel file: " + filepath, e);
        } catch (Throwable e) {
            throw new RuntimeException("cannot connect to excel file: " + filename, e);
        }
    }
}

我收到以下错误消息:

    Exception in thread "main" java.lang.RuntimeException: cannot connect to excel file: workbook1.xls
    at com.bfm.app.ldi.client.excel.dde.ExcelDdeExperiment.main(ExcelDdeExperiment.java:35)
Caused by: com.google.code.jdde.misc.DdeException: [DMLERR_NO_CONV_ESTABLISHED] A client's attempt to establish a conversation has failed.
    at com.google.code.jdde.ddeml.constants.DmlError.throwExceptionIfValidError(Unknown Source)
    at com.google.code.jdde.client.DdeClient.connect(Unknown Source)
    at com.bfm.app.ldi.client.excel.dde.ExcelDdeExperiment.main(ExcelDdeExperiment.java:28)

我首先尝试将完整的文件路径传递给

conversation = ddeclient.connect(filename, sheetname);

然后我尝试只传递文件名,在这两种情况下它都不起作用。

任何人都能指出我正确的方向吗?

亲切的问候

1 个答案:

答案 0 :(得分:0)

您可以尝试使用另一个库,例如http://jdde.pretty-tools.com/中具有相同名称的库(JDDE)。您可以在网站上找到示例。