首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi DDE 客戶端與 Excel 數據交換:自動/手動模式切換、數據請求發送、剪貼板建立聯接

     編輯:Delphi

7.3.8 利用客戶端程序與 Excel 交換數據

界面組件

  • DDEClientConv1、DDEClientItem1:建立/維護 DDE 聯接
  • RadioGroup + AutoRadio/ManualRadio:設置聯接模式
  • GroupBox + RequestBtn/PokeBtn:控制數據請求/發送(自動模式下 RequestBtn 禁用)
  • Memo1:保存 DDE 數據
  • PasteBtn:粘貼聯接信息建立 DDE 聯接
  • CloseBtn:關閉系統

設計時:FormatChars=True(保留服務器數據顯示格式),ConnectMode=ddeAutomatic(默認)。

狀態變量

類 TForm1 私有成員:Automatic: Boolean(聯接模式標誌)、DDEService/DDETopic/DDEItem(聯接信息)。

初始化與模式切換

procedure TForm1.FormCreate(Sender: TObject);
begin
  RequestBtn.Enabled := False;
  AutoRadio.Checked := True;
  Automatic := True;
end;

// 自動 → 手動
procedure TForm1.ManualRadioClick(Sender: TObject);
begin
  if Automatic then
  begin
    RequestBtn.Enabled := ManualRadio.Checked;
    DDEClientConv1.ConnectMode := ddeManual;
    Automatic := False;
  end;
end;

// 手動 → 自動
procedure TForm1.AutoRadioClick(Sender: TObject);
begin
  if not Automatic then
  begin
    RequestBtn.Enabled := ManualRadio.Checked;
    if (DDEService = '') or (DDETopic = '') then
    begin
      MessageDlg('Can not Set Link.', mtWarning, [mbOK], 0); Exit;
    end;
    DDEClientConv1.SetLink(DDEService, DDETopic);
    DDEClientItem1.DdeConv := DDEClientConv1;
    DDEClientItem1.DDEItem := DDEItem;
    DDEClientConv1.ConnectMode := ddeAutomatic;
    Automatic := True;
  end;
end;

自動→手動只需修改屬性;手動→自動必須調用 SetLink 重建聯接,否則易引發 DDE 異常。

剪貼板粘貼建立聯接

procedure TForm1.PasteBtnClick(Sender: TObject);
begin
  if GetPasteLinkInfo(DDEService, DDETopic, DDEItem) then
  begin
    DDEClientConv1.SetLink(DDEService, DDETopic);
    if Automatic then
    begin
      DDEClientItem1.DdeConv := DDEClientConv1;
      DDEClientItem1.DDEItem := DDEItem;
    end;
  end;
end;

GetPasteLinkInfo(DDEMan 單元)檢測剪貼板聯接信息並返回服務/主題/項目。

手動模式數據請求

手動模式下 DDEClientItem 多余,數據用字符串記錄。RequestBtnClick:

procedure TForm1.RequestBtnClick(Sender: TObject);
var TheData: PChar;
begin
  if DDEItem = '' then begin MessageDlg('Can not Request Data', mtWarning, [mbOK], 0); Exit; end;
  TheData := StrAlloc(79);
  DDEClientConv1.OpenLink;
  TheData := DDEClientConv1.RequestData(DDEItem);
  DDEClientConv1.CloseLink;
  if TheData <> nil then Memo1.Text := StrPas(TheData);
  StrDispose(TheData);
end;

OpenLink/CloseLink 打開/關閉聯接;RequestData 返回 PChar,須顯式分配/釋放內存。

數據發送(兩模式差異)

procedure TForm1.PokeBtnClick(Sender: TObject);
begin
  if DDEItem = '' then begin MessageDlg('Can not Poke Data.', mtWarning, [mbOK], 0); Exit; end;
  if Automatic then
    DDEClientConv1.PokeDataLines(DDEItem, Memo1.Lines)
  else
  begin
    DDEClientConv1.OpenLink;
    DDEClientConv1.PokeDataLines(DDEItem, Memo1.Lines);
    DDEClientConv1.CloseLink;
  end;
end;

手動模式額外增加 OpenLink/CloseLink。

操作步驟

  1. 打開 Excel,載入文件,選中相關單元格,複製到剪貼板
  2. 運行程序,點擊 Paste Link 建立 DDE 聯接,數據顯示於 Memo1
  3. 進行模式切換、數據請求、發送等操作
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved