In the case of Excel, I've used a slightly different approach and worked with the application object. The code creates a new Excel spreadsheet, fills it with a database table, and formats the result. It uses an Excel internal object, Range, which is not to be confused with a similar type available in Word (the reason this type is prefixed with the name of the unit defining the Excel type library). Here is the complete code:
procedure TFormOff.BtnExcelClick(Sender: TObject); var
RangeE: Excel97.Range; I, Row: Integer; Bookmark: TBookmarkStr; begin
// add field data in following rows
Bookmark := Table1.Bookmark; try
while not Table1.EOF do begin
RangeE := ExcelApplication1.Range ['A' + IntToStr (Row),
// format the section
RangeE := ExcelApplication1.Range ['A1', 'E' + IntToStr (Row - 1)]; RangeE.AutoFormat (3, NULL, NULL, NULL, NULL, NULL, NULL); end;
You can see the effect of this code in Figure 20.10. Notice that in the code I don't handle any events of the Office applications, but many are available. Handling these events was quite complex in the past, but they now become as simple to handle as events of native Delphi components. The presence of these events is a reason to have specific objects for documents and other specific elements: you might want to know when the user closes a document, and that therefore this is an event of the document object, not of the application object.
Was this article helpful?