BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TStringGrid and Clipboard

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Using
View previous topic :: View next topic  
Author Message
Henk Baas
Guest





PostPosted: Wed Oct 29, 2003 3:56 pm    Post subject: TStringGrid and Clipboard Reply with quote



Is it possible to copy a grid contents (in my case from a TStringGrid) to
the Clipboard and have it pasted in for example Excel?

Henk Baas


Back to top
Peter Below (TeamB)
Guest





PostPosted: Wed Oct 29, 2003 7:14 pm    Post subject: Re: TStringGrid and Clipboard Reply with quote



In article <3f9fe33e (AT) newsgroups (DOT) borland.com>, Henk Baas wrote:
Quote:
Is it possible to copy a grid contents (in my case from a TStringGrid) to
the Clipboard and have it pasted in for example Excel?

Of course.

Var
S: String;
i, k: Integer;
begin
S:= '';
with stringgrid1 do begin
for i:= FixedRows to RowCount-1 do begin
For k:= FixedCols to ColCOunt-1 do begin
If k > FixedCols Then
S:= S + #9;
S:= S + Cells[k,i];
end; { for }
S:= S + #13#10;
end; { for }
end; { with }
Clipboard.AsText := S;

This generates a string in which columns are separated by Tab characters
and rows by CR/LF linebreaks. Most spreadsheets are able to paste this into
cells correctly. The code is not exactly efficient, so if you want to use
this for really large grids you may want to change the logic to write the
data to a TMemoryStream first, it has a smarter "grow" method. You can copy
the content of the memory stream to a string via

SetString( aString, PChar( aMemorystream.Memory ), aMemoryStream.Size );

and then copy that to the clipboard. Or use my CopyStreamToClipboard
routine (search the newsgroup archives for that).

--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Using All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.