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 put data from a TStringGrid into an Excel spreadsheet 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
15-Oct-02
Category
VCL-General
Language
Delphi 2.x
Views
107
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to put data from a TStringGrid into an Excel spreadsheet

Answer:
1   
2   procedure TTradingForm.Button1Click(Sender: TObject);
3   var
4     i, j: Integer;
5     r, c: Integer;
6     v: OleVariant;
7     sRange: string;
8     WorkBook: _Workbook;
9     WorkSheet: _Worksheet;
10  
11    function ColToStr(ilCol: integer): string;
12    var
13      FirstLetter: integer;
14      lastLetter: integer;
15    begin
16      result := '';
17      firstLetter := (ilCol - 1) div 26 - 1;
18      lastLetter := (ilCol - 1) mod 26;
19      if firstLetter >= 0 then
20        result := chr(ord('A') + firstletter);
21      result := result + chr(ord('A') + lastLetter);
22    end;
23  
24  begin
25    v := VarArrayCreate([0, Grid.RowCount - 1, 0, Grid.ColCount - 1], varVariant);
26    for i := 0 to Grid.RowCount - 1 do
27    begin
28      for j := 0 to Grid.ColCount - 1 do
29        if i = 0 then
30          v[i, j] := Grid.Columns[j].Caption
31        else
32          v[i, j] := Grid.Cells[j, i];
33    end;
34    Screen.Cursor := crHourglass;
35    try
36      Excel.Connect;
37      Excel.Visible[GetUserDefaultLCID] := False;
38      WorkBook := Excel.Workbooks.Add(EmptyParam, GetUserDefaultLCID);
39      WorkBook.Activate(GetUserDefaultLCID);
40      Worksheet := Excel.ActiveWorkbook.Worksheets.Add(EmptyParam, EmptyParam, 2,
41        xlWBATWorksheet, GetUserDefaultLCID) as _Worksheet;
42      Excel.XLSelectWorksheet(1);
43      r := VarArrayHighBound(v, 2) + 1;
44      c := VarArrayHighBound(v, 1) + 1;
45      sRange := 'A1..' + ColToStr(r) + IntToStr(c);
46      Excel.XLSetRangeValue(sRange, v);
47    finally
48      Excel.Visible[GetUserDefaultLCID] := True;
49      Excel.Disconnect;
50      v := null;
51      Screen.Cursor := crDefault;
52    end;
53  end;


			
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