我尝试编写示例代码 - 最简单的,我收到连接错误。
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);
然后我尝试只传递文件名,在这两种情况下它都不起作用。
任何人都能指出我正确的方向吗?
亲切的问候