delphi StringGrid 表格的复制粘贴

//参考如下代码~~
uses Clipbrd;

function StringGridSelectText(mStringGrid: TStringGrid): string;
var
I, J: Integer;
S: string;
begin
Result := ”;
if not Assigned(mStringGrid) then Exit;
for J := mStringGrid.Selection.Top to mStringGrid.Selection.Bottom do
begin
S := ”;
for I := mStringGrid.Selection.Left to mStringGrid.Selection.Right do
S := S + #9 + mStringGrid.Cells[I, J];
Delete(S, 1, 1);
Result := Result + S + #13#10;
end;
end; { StringGridSelectText }

procedure StringGridPasteFromClipboard(mStringGrid: TStringGrid);
var
vTextList: TStringList;
vLineList: TStringList;
I, J: Integer;
begin
vTextList := TStringList.Create;
vLineList := TStringList.Create;
vLineList.Delimiter := #9;
try
vTextList.Text := Clipboard.AsText;
for J := 0 to vTextList.Count – 1 do
begin
if J + mStringGrid.Row >= mStringGrid.RowCount then Break;
vLineList.DelimitedText := vTextList[J];
for I := 0 to vLineList.Count – 1 do
begin
if I + mStringGrid.Col >= mStringGrid.ColCount then Break;
mStringGrid.Cells[I + mStringGrid.Col, J + mStringGrid.Row] := vLineList[I];
end;
end;
finally
vTextList.Free;
vLineList.Free;
end;
end; { StringGridPasteFromClipboard }

procedure StringGridCopyToClipboard(mStringGrid: TStringGrid);
begin
Clipboard.AsText := StringGridSelectText(mStringGrid);
end; { StringGridCopyToClipboard }

procedure TForm1.MenuItemCopyClick(Sender: TObject);
begin
StringGridCopyToClipboard(StringGrid1);
end;

procedure TForm1.MenuItemPasteClick(Sender: TObject);
begin
StringGridPasteFromClipboard(StringGrid1);
end;

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if ssCtrl in Shift then
case Key of
Ord(‘C’): StringGridCopyToClipboard(TStringGrid(Sender));
Ord(‘V’): StringGridPasteFromClipboard(TStringGrid(Sender));
end;
end;

Original: https://www.cnblogs.com/zhangzhifeng/p/9024491.html
Author: ╰★张志峰★╮
Title: delphi StringGrid 表格的复制粘贴

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/548964/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球