Excel からクリップボードにコピーすると、さまざまな形式がクリップボードに配置されます。複製でき、目的の結果が得られるフォーマットの 1 つを見つける必要があります。
過去にこれを達成した方法は、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');
あとは、その関数に渡す HTML を生成するだけです。それはあなた次第です。Excel を使用して HTML をクリップボードに置き、生成された HTML を検査することをお勧めします。あなたがする必要があることのガイドとしてそれを使用してください。