Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to copy selected data from a TStringGrid to the clipboard Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Copy selected data from a TStringGrid to the clipboard 05-Nov-02
Category
VCL-General
Language
Delphi 2.x
Views
117
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			 Author: Tomas Rutkauskas

I have various TStringGrid objects within my application and I want to allow the 
user to copy selected data to the clipboard for insertion into other programs such 
as Excel.

Answer:

Solve 1:

1   var
2     S: string;
3     i, k: Integer;
4   begin
5     S := EmptyStr;
6     with StringGrid1 do
7     begin
8       for i := FixedRows to RowCount - 1 do
9       begin
10        for k := FixedCols to ColCount - 1 do
11        begin
12          if k > FixedCols then
13            S := S + #9;
14          S := S + Cells[k, i];
15        end;
16        S := S + #13#10;
17      end;
18    end;
19    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.

Same for the selection in a grid:

20  S := '';
21  with grid do
22    for i := Selection.Top to Selection.Bottom do
23    begin
24      for k := Selection.Left to Selection.Right do
25      begin
26        S := S + Cells[k, i];
27        if k <> Selection.Right then
28          S := S + #9;
29      end;
30      S := S + #13#10;
31    end;
32  Clipboard.AsText := S;



Solve 2:

Here is how you can copy a selection.

33  uses
34    ClipBrd;
35  
36  procedure CopyGridSelectionToClipBoard(Grid: TStringGrid; Selection: TGridRect);
37  const
38    TAB = Chr(VK_TAB);
39    CR = #13;
40  var
41    r, c: integer;
42    S: string;
43  begin
44    S := '';
45    for r := Selection.Top to Selection.Bottom do
46    begin
47      for c := Selection.Left to Selection.Right do
48      begin
49        S := S + Grid.Cells[c, r];
50        if c < Selection.Right then
51          S := S + TAB;
52      end;
53      if r < Selection.Bottom then
54        S := S + CR;
55    end;
56    ClipBoard.SetTextBuf(PChar(S));
57  end;


Pasting would be the reverse. An idea would be to get the text from the clipboard 
and assign it to a TStringList. This way you'll know how many rows you have.

58  var
59    F: TStringList;
60  begin
61    F := TStringList.Create;
62    F.Text := Clipboard.AsText;
63    F.Free;
64  end;


You'll still need to parse each row to get the columns.

By the way, you can use the text you copied with the above procedure to paste into Excel. Excel knows how to parse it if you use the TAB to delimit columns.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC