procedure Tfrm_Main.BitBtn4Click(Sender: TObject);
var excelcaption:string;
begin
if (CYear.Text<>'') and (CMonth.Text<>'') and (CDay.Text<>'') then
begin
DataModule1.AQ_Total.First;
BitBtn2.Enabled:=True;
BitBtn4.Enabled:=True;
excelcaption:=CYear.Text + '年' + CMonth.Text + '月' + CDay.Text + '日——' + CYear2.Text + '年' + CMonth2.Text + '月' + CDay2.Text + '日';
frmToExcel :=TfrmToExcel.Create(Self);
frmToExcel.SaveToExcel('结算汇总' + '(' + excelcaption +')',DBGrid1);
frmToExcel.ShowModal;
frmToExcel.Free;
end
else
begin
Showmessage('结算汇总的起始日期或者结束日期输入有误!');
BitBtn2.Enabled:=False;
BitBtn4.Enabled:=False;
end;
end;
--------------------------------
procedure TfrmToExcel.SaveToExcel(T1: String; DBG: TDBGrid);
var
XLApp:Variant;
DS:TDataset;
Const
xlWBATWorksheet = -4167;
var
i,j,ii: Integer;
Sheet,jj: Variant;
begin
Screen.Cursor := crHourGlass;
DS := DBG.DataSource.Dataset;
XLApp := CreateOleObject('Excel.Application');
XLApp.Visible := True;
XLApp.Workbooks.Add(xlWBatWorkSheet);
XLApp.Workbooks[1].WorkSheets[1].Name := T1;
Sheet:=XLAPP.Workbooks[1].WorkSheets[T1];
ii:=DS.FieldCount-3;
for i:=0 to ii-1 do
begin
jj:=DBG.Columns.Items.Title.Caption;
Sheet.Cells[3, i+1] := jj;
end;
i:=4;
While not DS.Eof do
begin
Sheet.Cells[i,1]:=i-2;
for j:=0 to DS.FieldCount-4 do
begin
jj:=DS.Fields[j].Value ;
Sheet.Cells[i, j+1] := jj;
end;
DS.next;
i:=i+1;
end;
LblMsg.Caption :='保存完毕!';
LblMsg.Refresh ;
if not BitBtn3.Enabled then
BitBtn3.Enabled :=true;
Screen.Cursor := crDefault;
end;