[DELPHI]TQuery的结果到入Excel

[Crystal Studio Web][DELPHI]TQuery的结果到入Excel
TQuery的结果到入Excel

作者:chinawzw 推荐:chinawzw
--------------------------------------------------------------------------------

unit ExcelTest;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls,db,DBTables, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Query1: TQuery;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure WriteDatasetToExcel(AQueryName: TQuery; AStrVar: String);
end;

var
Form1: TForm1;

implementation

uses Comobj;

{$R *.DFM}

{ TForm1 }

procedure TForm1.WriteDatasetToExcel(AQueryName: TQuery; AStrVar: String);
var
EclApp,WorkBook : Variant;
xlsFileName : String ;
I : Integer ;
column : Integer ;
Row : Integer ;
Fdate:TDateTime;
Year, Month, Day, Hour, Min, Sec, MSec: Word;
StrDate:String ;
StrDate1:String ;
Begin
Fdate:=now ;
DecodeDate(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文件测试' Then
Begin
xlsfilename :='Excel文件测试' ;
End ;

Try
Begin
EclApp := CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
End
Except
ShowMessage('您的计算机上没有 Microsoft Excel!');
Exit;
end;
try
workBook:=EclApp.workBooks.Add ;
row:=2;
EclApp.Workbooks.Item[1].Activate;
eclApp.Cells.font.colorindex:=5 ;
EclApp.Activesheet.Cells(1,1):=AStrVar ;
For I := 1 To AQueryName.FieldCount Do
EclApp.Activesheet.Cells(2,I):=AQueryName.Fields[I-1].FieldName ;
If Not AQueryName.Active Then AQueryName.Active := True ;
AQueryName.First ;
While Not(AQueryName.Eof) do
begin
column:=1;
for i:=1 to AQueryName.FieldCount do
begin
eclApp.Cells.Item[row+1,column]:=AQueryName.fields[i-1].AsString;
column:=column+1;
end;
AQueryName.Next;
row:=row+1;
End ;
WorkBook.saveas(xlsFileName);
WorkBook.close;
WorkBook:=eclApp.workBooks.Open(xlsFileName);
if MessageDlg('xlsFileName'+'对该文件是否保存?',
mtConfirmation,[mbYes, mbNo], 0) = mrYes then
WorkBook.save
Else
workBook.Saved := True;
WorkBook.Close;
eclApp.Quit;
eclApp:=Unassigned;
except
ShowMessage('Excel 文件保存失败');
WorkBook.close;
eclApp.Quit; {释放VARIANT变量}
eclApp:=Unassigned;
end;
ShowMessage('EXCEL 文件保存完毕') ;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
WriteDatasetToExcel(query1,'Excel文件测试');
end;

end.
使用方便,功能强大。 type FileCheckResult = (fcrNotExistend,fcrNotXSLFile,fcrValidXSL); //文件不存在,不是XSL文件,合法的XSL文件 TOLEExcel = class(TComponent) private FExcelCreated: Boolean; FVisible: Boolean; FExcel: Variant; //Excel程序对象 FWorkBook: Variant; //Excel工作簿对象 FWorkSheet: Variant; //Excel工作簿 工作表对象 FCellFont: TFont; //单元格字体对象 FTitleFont: TFont; // FFontChanged: Boolean; FIgnoreFont: Boolean; FFileName: TFileName; //********************************************自己添加*****************************// FCreateFromFile:Boolean; //指示是否打开已有文件 FExcelCaption:string; //用程序打开Excel的窗体标 //*********************************来自U_Report*****************************// FRCPrePage:Integer; //每页显示的记录数 FMax:Integer; //最大的数组个数 procedure SetExcelCellFont(var Cell: Variant); procedure SetExcelTitleFont(var Cell: Variant); procedure GetTableColumnName(const Table: TTable; var Cell: Variant); procedure GetQueryColumnName(const Query: TQuery; var Cell: Variant); procedure GetFixedCols(const StringGrid: TStringGrid; var Cell: Variant); procedure GetFixedRows(const StringGrid: TStringGrid; var Cell: Variant); procedure GetStringGridBody(const StringGrid: TStringGrid; var Cell: Variant); protected procedure SetCellFont(NewFont: TFont); procedure SetTitleFont(NewFont: TFont); procedure SetVisible(DoShow: Boolean); function GetCell( ARow,ACol: Integer): string; procedure SetCell(ACol, ARow: Integer; const Value: string); function GetDateCell(ACol, ARow: Integer): TDateTime; procedure SetDateCell(ACol, ARow: Integer; const Value: TDateTime); //*********************************************自己添加************************************// procedure SetCaption(ACaption:string);//设置打开文件后,Excel主程序的窗体标题 function GetCapiton:string;//返回打开文件后,Excel主程序的窗体标题 public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure C
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值