Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 08-10-2018, 12:57 PM
GeoWink GeoWink is offline
Senior Member
 
Join Date: Jan 2001
Posts: 882
Default Create and save Excel file

I have created an Excel spreadsheet in my program but not sure how to save it to the desktop. Here is what I did...

procedure TForm1.Button1Click(Sender: TObject);
var
arrData: Variant;
xls, wb, Range: OLEVariant;
i,maxi:integer;
begin
with HunterQuery do
begin
Close;
SQL.Clear;
SQL.ADD('select * from mainfile T');
SQL.ADD('Order by T.LastName, T.FirstName');
EnableControls;
Open;
maxi:=RecordCount;
{create variant array where we'll copy our data}
arrData := VarArrayCreate([1, maxi+3, 1,5], varVariant);
arrData[1, 1] :='FirstName';
arrData[1, 2] :='LastName';
arrData[1, 3] :='CellPhone';
arrData[1, 4] :='HomePhone';
arrData[1, 5] :='EMail';
FindFirst;
for i := 1 to maxi do
begin
arrData[i+1, 1] :=FieldByName('FirstName').asString;
arrData[i+1, 2] :=FieldByName('LastName').asString;
arrData[i+1, 3] :=FieldByName('CellPhone').asString;
arrData[i+1, 4] :=FieldByName('HomePhone').asString;
arrData[i+1, 5] :=FieldByName('EMail').asString;
Application.ProcessMessages;
if i<>maxi then FindNext;
end;
end;
xls := CreateOLEObject('Excel.Application');
wb := xls.Workbooks.Add;
Range := wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[1, 1],
wb.WorkSheets[1].Cells[maxi+1, 5]];
Range.Value := arrData;
end;

Any help will be greatly appreciated. Thanks.

George
Reply With Quote
  #2  
Old 08-12-2018, 02:44 AM
GeoWink GeoWink is offline
Senior Member
 
Join Date: Jan 2001
Posts: 882
Default

I answered this myself.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:09 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.