Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does one copy data to Excell? 3

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA
Is there a guru out there who can advise how to copy the date from (say) MyTableMYFIELD.value to (say) Book1.Sheet1.A1 in MyExcell.xls?

Thanks in advance.
 
To Steve Van Els.

Before you respond to this I have just found your very informative forum102 which deals with this.

Thanks mate! And a great 2004 to you!!

 
Here's some snippets of another option that allows you to fill an array and transfer the entire array to a sheet in a workbook:
Code:
var
ArrayOfData : variant;
excelapp, excelsht: variant;
begin
//create an array
ArrayOfData := VarArrayCreate([0, 100, 0, 2], varOleStr);
//fill the array
for i := 1 to 99 do
  begin
    ArrayOfData [i, 0] := qry.FieldByName('Field1').AsString;
    ArrayOfData [i, 1] := qry.FieldBYName('Field2').AsString;
    qry.Next;
  end; 

//initialize Excel 
  excelapp:= CreateOleObject('Excel.Application');
  excelapp.Visible := False;
  excelapp.Workbooks.Add;
  excelsht :=  excelapp.WorkSheets.Item['Sheet1'];
  excelsht.Activate;

//transfer entire array to spreadsheet
  excelsht.Range[excelsht.Cells.Item[1, 1], excelsht.Cells.Item[(100), 2]].Value := ArrayOfData;

//save and close
  excelapp.displayalerts := False;
  excelsht.SaveAs('R:\Labels.xls');
  excelapp.Workbooks.Close;
  excelapp.quit;
end;




Leslie
 
Delphi includes its own wrapper objects
TExcelApplication
TExcelWorkbook
TExcelWorkSheet
etc

Although frankly I find Leslie's approach of using the commands directly easier.

Out of the box Delphi also includes Formula 1 (TF1Book). A lighter weight spreadsheet which will save and read Excel compatible .xls files. Just gives another option.

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top