How to paste data from Excel into cxGrid

时光怂恿深爱的人放手 提交于 2019-12-23 01:20:31

问题


I have a Delphi application using DevExpress cxGrid (which is connected to database).

I require to be able to copy-paste data from Excel into the grid.

Is this possible? If so, how to do it, which additional components do i need?


回答1:


Check the format with Clipboard.HasFormat(CF_TEXT).

Extract the text with Clipboard.AsText.

Split into rows with StringList.Text := Clipboard.AsText. Each item in the string list is now a row from the clipboard.

Split each row into individual cells using a Split function:

function Split(const s: string; Separator: char): TStringDynArray;
var
  i, ItemIndex: Integer;
  len: Integer;
  SeparatorCount: Integer;
  Start: Integer;
begin
  len := Length(s);
  if len=0 then begin
    Result := nil;
    exit;
  end;

  SeparatorCount := 0;
  for i := 1 to len do begin
    if s[i]=Separator then begin
      inc(SeparatorCount);
    end;
  end;

  SetLength(Result, SeparatorCount+1);
  ItemIndex := 0;
  Start := 1;
  for i := 1 to len do begin
    if s[i]=Separator then begin
      Result[ItemIndex] := Copy(s, Start, i-Start);
      inc(ItemIndex);
      Start := i+1;
    end;
  end;
  Result[ItemIndex] := Copy(s, Start, len-Start+1);
end;



回答2:


You can check the content of the clipboard after you copy content from the Excel app. and then on CTRL+V shortcut you parse the content of the clipboard and set data to their respective cells.



来源:https://stackoverflow.com/questions/4358064/how-to-paste-data-from-excel-into-cxgrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!