View Single Post
 
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