How to Copy Data from Grid and paste it into Excel
Friday, July 22nd, 2011If you want to copy some lines of data from a Grid (Ctrl+C) and paste (Ctrl+V) it into an Excel document, just use following code:
procedure MyForm.Button1Click(Sender: TObject);
var
i, j: integer;
lString: string;
begin
lString := '';
for i := 0 to Grid.RowCount - 1 do begin
for j := 0 to Grid.ColCount - 1 do begin
lString := lString + Grid.Cells[j, i];
if j < Grid.ColCount - 1 then
lString := lString + #9;
end;
if i < Grid.RowCount - 1 then
lString := lString + #13;
end;
Clipboard.AsText := lString;
end;
With few modifications this code can be also applied to TDrawGridEx.
