I am creating a Delphi program that needs to extract data from our database and create an Excel report. I would prefer to use the Delphi components TExcelApplication, TExcelWorkbook and TExcelWorksheet, but I haven't been able to get them to work. Most of the examples I have found are fairly old and I don't know if that's the problem or not.

Can anyone point me to a tutorial or example that uses these components in Delphi to create an Excel spreadsheet, populate that spreadsheet (I will be using data from the database), and then save that spreadsheet as a given filename?

I have been able to get fairly far using CreateOleObject('Excel.Application') to return me a variant, but then I'm running fairly blind not having any documentation and using just trial and error to make any progress.

Using the components, I have only gotten this far:

oXL.Connect;
  ExcelApplication1.Visible[0] := True;
  ExcelApplication1.Caption := 'Terry''s Special Excel Application';

  ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[0]);  <= This dies: Invalid Index
  ExcelWorksheet1.ConnectTo(ExcelWorkBook1.ActiveSheet as _Worksheet);
  ExcelApplication1.UserControl := True;

Try to start counting from 1. I have not worked with the components, always use another method. I know that almost all the indices in the COM Excel ticking is from 1.

Try to start counting from 1. I have not worked with the components, always use another method. I know that almost all the indices in the COM Excel ticking is from 1.

Thanks for your reply. I first tried "0" but got the same index error.

I think the traditional approach is simpler.

var
  fObject: Variant;
begin
 fObject := CreateOleObject('Excel.Application');
  if not VarIsEmpty(fObject) then begin
    fObject.Visible := true;
    fObject.IgnoreRemoteRequests := True;
    fObject.WorkBooks.Open(FileName, ReadOnly:=false);
    fObject.ActiveSheet.Cells[1, 1].Text := 'Hello world';
end;

I think the traditional approach is simpler.

I would agree except then one has no idea what calls are available. How do I find out how to turn a block of cells into a formatted table in Excel 2010?

This code works the same in office XP,2003,2007. There does not specify the version of office. Checked it works. If Microsoft what it has not changed that will work in 2010.To clarify the version you want to use something like "Excel.Application.12"

This article has been dead for over six months. Start a new discussion instead.