将多个单元格复制到excel格式的剪贴板?

时间:2022-04-25 09:43:41

I'm working on a tool which connects to a SQL Database, gets back a dataset, and displays that data in a grid. The user must be able to select a block of cells (just rectangular) and press CTRL+C to copy it to the clipboard.

我正在开发一个工具,该工具连接到SQL数据库,获取数据集,并在网格中显示数据。用户必须能够选择一个单元格块(只是矩形)并按CTRL+C将其复制到剪贴板。

How do I do this:

我该怎么做:

  • In a format that can be pasted into Excel? I'm hoping there's already something ready-made for this. It doesn't need all the clipboard features like Excel, just highlighting a rectangular group of cells and copying it to the clipboard.

    可以粘贴到Excel中的格式?我希望已经有现成的了。它不需要像Excel那样的所有剪贴板功能,只需要突出显示一个矩形的单元格并将其复制到剪贴板。

  • If it can be done in a TStringGrid I would prefer to keep my functionality in that, but could also work with a component which supports this.

    如果可以在TStringGrid中完成,我宁愿将功能保留在其中,但也可以使用支持这一点的组件。

1 个解决方案

#1


6  

You can try to copy you cell values as TAB delimited text, something like this code does:

你可以试着把你的单元格值复制到标签分隔的文本中,类似于这样的代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
  X, Y: Integer;
begin
  S := '';
  for Y := StringGrid1.Selection.Top to StringGrid1.Selection.Bottom do
  begin
    for X := StringGrid1.Selection.Left to StringGrid1.Selection.Right - 1 do
      S := S + StringGrid1.Cells[X, Y] + #9;
    S := S + StringGrid1.Cells[StringGrid1.Selection.Right, Y] + sLineBreak;
  end;
  Delete(S, Length(S) - Length(sLineBreak) + 1, Length(sLineBreak));
  Clipboard.AsText := S;
end;

#1


6  

You can try to copy you cell values as TAB delimited text, something like this code does:

你可以试着把你的单元格值复制到标签分隔的文本中,类似于这样的代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
  X, Y: Integer;
begin
  S := '';
  for Y := StringGrid1.Selection.Top to StringGrid1.Selection.Bottom do
  begin
    for X := StringGrid1.Selection.Left to StringGrid1.Selection.Right - 1 do
      S := S + StringGrid1.Cells[X, Y] + #9;
    S := S + StringGrid1.Cells[StringGrid1.Selection.Right, Y] + sLineBreak;
  end;
  Delete(S, Length(S) - Length(sLineBreak) + 1, Length(sLineBreak));
  Clipboard.AsText := S;
end;