发布网友 发布时间:2022-04-23 00:39
共1个回答
热心网友 时间:2023-06-28 15:20
procere Tchu.BitBtn1Click(Sender: TObject);
var
i,row,column,icount:integer;
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
begin
dm1.ADOQuery5.close;
dm1.ADOQuery5.SQL.Clear ;
dm1.ADOQuery5.SQL.text:='select * from 记录 where 日期 between #'+formatdatetime('yyyy-mm-dd',datetimepicker1.date)+'# and #'+formatdatetime('yyyy-mm-dd',datetimepicker2.date)+'# ';
dm1.ADOQuery5.Open;
//导出
Try
ExcelApplication1:=TExcelApplication.Create(Application);
ExcelWorksheet1:=TExcelWorksheet.Create(Application);
ExcelWorkbook1:=TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
Except
MessageDlg('Excel没有安装!',
mtError, [mbOk], 0);
Abort;
End;
//ExcelApplication1.Visible[0] := false; //不显示过程
ExcelApplication1.Visible[0]:=true; //显示过程
ExcelApplication1.Caption:='Excel Application';
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
DBGrid1.DataSource.DataSet.Open;
row := 2;
DBGrid1.DataSource.DataSet.first;
for iCount:= 0 to DBGrid1.Columns.Count-1 do
begin
ExcelWorksheet1.cells.Item [1,iCount+1]:=DBGrid1.Columns.Items[iCount].Title.Caption;
end;
While Not (DBGrid1.DataSource.DataSet.Eof) do
begin
column:=1;
for i :=1 to DBGrid1.DataSource.DataSet.FieldCount do
begin
ExcelWorksheet1.Cells.Item[row,column]:=DBGrid1.DataSource.DataSet.fields[i-1].AsString;
column:=column+1;
end;
DBGrid1.DataSource.DataSet.Next;
row:=row+1;
end;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Disconnect;
//ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
messagebox(getactivewindow(),'导出结算数据到EXCEL成功!','提示',MB_OK+MB_ICONINFORMATION);
end ;