以Excel格式将多个单元格从网格复制到剪贴板?

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

我该怎么做呢:

  • 在可以粘贴到Excel中的格式? 我希望已经有这样的东西了。 它不需要像Excel这样的剪贴板function,只需突出显示矩形的一组单元格并将其复制到剪贴板即可。

  • 如果可以在TStringGrid完成,我宁愿保留我的function,但也可以使用支持此function的组件。

您可以尝试将单元格值复制为TAB delimited text ,如下所示:

 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;