2012-04-24 41 views
0

我試着寫了一個示例代碼 - 最簡單的可能,我得到一個連接錯誤。使用jDDE將Java連接到Excel的問題

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

然後,我嘗試只通過文件名,在這兩種情況下,沒有工作。

任何人都可以指出我在正確的方向嗎?

親切的問候

+0

你有沒有想過這個?該文件用上面的代碼打開很好,但connect方法的預期的第一個參數是服務器上運行的服務名稱。你有沒有運行一個服務,或者你只是在使用本地的excel文件? – demongolem 2012-12-20 15:36:28

+0

這爲我工作 - 我不得不改變文件名,我想,不記得了 – Charbel 2013-01-09 09:56:43

回答