Copy multiple cells from grid to clipboard in Excel format?

I am working on a tool that connects to an SQL database, returns a dataset and displays that data in a grid. The user should be able to select a block of cells (only a rectangle) and press CTRL + C to copy it to the clipboard.

How to do it:

  • In a format that can be pasted into Excel? I hope that there is already something ready for this. He does not need all the functions of the clipboard, such as Excel, just selecting a rectangular group of cells and copying it to the clipboard.

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

+4
source share
1 answer

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

 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; 
+6
source

All Articles