Son*_*rey 2 delphi excel clipboard copy
我正在使用一堆网格,这些网格不支持像 Excel 那样使用表格布局复制行和列。
我们需要能够从网格中复制一些行和列,并将它们粘贴到 Outlook 电子邮件中,并在正确对齐的列中使用正确的格式。如果您从 Excel 复制,效果会很好。
如果我从网格复制,我会得到制表符分隔的数据,但这是行不通的。此外,字体不像 Courier 那样是等宽字体,因此将数据填充到相同数量的字符也不起作用。
我真的很想知道 Excel 如何设法将这种额外的格式放入剪贴板。顺便说一句,我正在使用 Delphi,但任何建议都会受到赞赏。
编辑:我们不想先通过 Excel...我们想直接从网格到剪贴板,然后到电子邮件。
谢谢!巴特
当您从 Excel 复制到剪贴板时,剪贴板上会放置许多不同的格式。您需要找到一种可以复制的格式,这将给出所需的结果。
我过去实现这一目标的方法是将 HTML 放在剪贴板上。为此,您可以使用此功能:
procedure ClipboardError;
begin
raise EMyExceptionClass.Create('Could not complete clipboard operation.');
end;
procedure CheckClipboardHandle(Handle: Windows.HGLOBAL);
begin
if Handle=0 then begin
ClipboardError;
end;
end;
procedure CheckClipboardPtr(Ptr: Pointer);
begin
if not Assigned(Ptr) then begin
ClipboardError;
end;
end;
procedure PutInClipboard(ClipboardFormat: UINT; Buffer: Pointer; Count: Integer);
var
Handle: Windows.HGLOBAL;
Ptr: Pointer;
begin
if Count>0 then begin
Clipboard.Open;
Try
Handle := Windows.GlobalAlloc(GMEM_MOVEABLE, Count);
Try
CheckClipboardHandle(Handle);
Ptr := Windows.GlobalLock(Handle);
CheckClipboardPtr(Ptr);
Move(Buffer^, Ptr^, Count);
Windows.GlobalUnlock(Handle);
Clipboard.SetAsHandle(ClipboardFormat, Handle);
Except
GlobalFree(Handle);
raise;
End;
Finally
Clipboard.Close;
End;
end;
end;
var
HTMLClipboardFormat: UINT;
procedure PutHTMLInClipboard(Strings: TStrings);
var
Data: TStringList;
procedure WriteDescription(const StartOffset, EndOffset: Integer);
begin
while Data.Count<5 do begin
Data.Add('');
end;
Data[0] := 'Version:0.9';
Data[1] := Format('StartHTML:%.8d', [StartOffset]);
Data[2] := Format('EndHTML:%.8d', [EndOffset]);
Data[3] := Format('StartFragment:%.8d', [StartOffset]);
Data[4] := Format('EndFragment:%.8d', [EndOffset]);
end;
var
StartOffset, EndOffset: Integer;
Text: UTF8String;
begin
Data := TStringList.Create;
Try
WriteDescription(0, 0);//write out description stub - will be replaced later
StartOffset := Length(UTF8String(Data.Text));
Data.AddStrings(Strings);
EndOffset := Length(UTF8String(Data.Text))-1;
WriteDescription(StartOffset, EndOffset);//now we know the offsets we can write proper description
Text := Data.Text;
PutInClipBoard(HTMLClipboardFormat, PAnsiChar(Text), Length(Text));
Finally
FreeAndNil(Data);
End;
end;
....
initialization
HTMLClipboardFormat := Windows.RegisterClipboardFormat('HTML Format');
Run Code Online (Sandbox Code Playgroud)
唯一剩下的就是生成传递给该函数的 HTML。这取决于你。我建议您使用 Excel 将 HTML 放入剪贴板,然后检查它生成的 HTML。将其用作您需要执行的操作的指南。
| 归档时间: |
|
| 查看次数: |
5319 次 |
| 最近记录: |