[Crystal Studio Web] [DELPHI] TQuery results into the results into Excel to ExcelTQuery Author: chinawzw Recommended: chinawzw ------------------------- -------------------------------------------------- ----- unit ExcelTest; interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, db, dBTables, ComCtrls; typeTForm1 = class (TForm) Button1: TButton; Query1: TQuery; procedure Button1Click (Sender: TObject); private {Private declarations} public {public declarations} procedure WriteDatasetToExcel (AQueryName: TQuery; AStrVar: String); end; varForm1: TForm1; implementationuses Comobj; {$ R * .DFM} {TForm1} procedure TForm1.WriteDatasetToExcel (AQueryName: TQuery; AStrVar: String); varEclApp, WorkBook: Variant; xlsFileName: String; I: Integer; column: Integer; Row: Integer; Fdate: TDateTime; Year, Month, Day, Hour, Min, Sec , Msec: word; strdate: string; strdate1: string; beginfdate: = now; decodate (fdate, year, month, day); decodetime (Fdate, Hour, min, sec, msec); strdate: = formatdatetime ('yyyy- MM-DD- HH-mm-ss', fdate; strdate1: = formatdatetime ('YYYY / MM / DD HH: MM: SS', FDATE); if astrvar = 'Excel file test' theNbeginxlsFileName: = 'Excel file test'; TryBeginEclApp: = CreateOleObject ( 'Excel.Application'); WorkBook: = CreateOleObject ( 'Excel.Sheet'); EndExceptShowMessage ( 'no Microsoft Excel on your computer!'); Exit; end; tryworkBook: = EclApp.workBooks.Add Row: = 2; ECLAPP.WORKBOOKS.ITEM [1] .actiVate; eclapp.cells.font.colorindex: = 5; eclapp.activesheet.cells (1): = astrvar; for i: = 1 to AQueryName. Fieldcount doeclapp.activeesheet.cells (2, i): = AQueryName.fields [i-1] .fieldname; if not aqueryname.active dam, aqueryname.active: = true; AQueryName.First;