如何将 Excel 中的数据粘贴到 cxGrid 中

发布于 2024-10-05 17:51:55 字数 124 浏览 0 评论 0原文

我有一个使用 DevExpress cxGrid 的 Delphi 应用程序(已连接到数据库)。

我需要能够将数据从 Excel 复制粘贴到网格中。

这可能吗?如果是这样,该怎么做,我需要哪些额外组件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潜移默化 2024-10-12 17:52:58

从 Excel 应用程序复制内容后,您可以检查剪贴板的内容。然后使用 CTRL+V 快捷键解析剪贴板的内容并将数据设置到各自的单元格中。

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.

春庭雪 2024-10-12 17:52:49

使用Clipboard.HasFormat(CF_TEXT)检查格式。

使用Clipboard.AsText提取文本。

使用 StringList.Text := Clipboard.AsText 拆分为行。字符串列表中的每个项目现在都是剪贴板中的一行。

使用 Split 函数将每一行拆分为单独的单元格:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文