我写过一段代码,我的excel进程不会驻留内存,你可以试一试!
var
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
filename: string;
filename_xls: string;
i,j,ii,jj : integer;
confirm :word;
CurrentFile :word;
pathname :string;
aaa :integer;
bbb,weekint:integer;
begin
Form1.Button1Click(self);
with dm do
begin
IF Querytemp.Active THEN
IF Querytemp.RecordCount > 0 THEN
BEGIN
//confirm:=messagedlg('您要导出考勤月报表记录吗?',mtwarning,[mbyes,mbno,mbcancel],0);
//if confirm=mryes then
begin
if SaveDialog1.Execute then
begin
aaa :=LastDelimiter('/',''''+SaveDialog1.FileName+'''');
bbb :=length(''''+SaveDialog1.FileName+'''');
PathName :=copy(''''+SaveDialog1.FileName+'''', 2,aaa-1);
filename := copy(''''+SaveDialog1.FileName+'''',aaa+1,bbb-aaa-1);
end;
//filename := 'ttt';
if filename <> '' then
BEGIN
PANEL3.Visible := TRUE;
PANEL3.Update;
///########################################################################################
///########################################################################################
filename_xls := concat(extractfilepath(application.exename), 'M0001', '.xls');
try
///////////////////////////////////////////////
Excel := CreateOleObject( 'Excel.Application' );
///////////////////////////////////////////////
except
end;
try
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
//showmessage('ok');
except
Application.Messagebox('Excel 没有安装!', 'Hello', MB_ICONERROR + mb_Ok);
Abort;
end;
try
//ExcelApplication1.Workbooks.Add(EmptyParam, 0);
//ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
//ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
//filename := '';
//showmessage('1');
//
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks.Open(filename_xls,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0));
ExcelWorkSheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
//初始化表头
ExcelWorksheet1.Cells.item[2, 1] := v_company+COPY(YYYYMM.Text,1,4)+'年'+COPY(YYYYMM.Text,6,2)+' 月考勤月报表';
ExcelWorksheet1.Cells.item[1, 1] := DATETOSTR(SDATE.Date)+'到'+DATETOSTR(EDATE.Date)+'员工考勤结果报表';
//写入数据到EXCEL
Excel.Columns['B:B'].NumberFormatLocal:='@';
i:=5;
//第一列写序列号
//ExcelWorksheet1.Cells.item[i, 1] := '序号';
//第二列写工号
//ExcelWorksheet1.Cells.item[i, 2] := '工号';
//第三列写名字
//ExcelWorksheet1.Cells.item[i, 3] := '姓名';
j:=5;
while j < DBGrid1.FieldCount+1 do
begin
//ExcelWorksheet1.Cells.item[i, 1] := ExcelWorksheet1.Rows.;
ExcelWorksheet1.Cells.item[i, j-1] :=DBGrid1.Fields[j-1].DisplayLabel;
//标示星期几
weekint := 40;
try
weekint := strtoint(DBGrid1.Fields[j-1].DisplayLabel);
except
weekint := 40;
end;
//if DBGrid1.Fields[j-1].DisplayLabel in ('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31') then
if weekint < 40 then
begin
if v_dd = '01' then
begin
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 1 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '日';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 2 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '一';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 3 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '二';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 4 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '三';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 5 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '四';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 6 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '五';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 7 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '六';
end
else
if strtoint(DBGrid1.Fields[j-1].DisplayLabel) > strtoint(v_dd) then
begin
if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 1 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '日';
if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 2 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '一';
if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 3 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '二';
if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 4 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '三';
if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 5 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '四';
if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 6 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '五';
if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 7 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '六';
end
else
begin
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 1 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '日';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 2 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '一';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 3 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '二';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 4 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '三';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 5 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '四';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 6 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '五';
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 7 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '六';
end;
end;
inc(j)
end;
ExcelWorksheet1.Cells.item[3, 1] := '统计部门:'+TreeView1.Selected.Text;
//写入数据到EXCEL
Querytemp.First;
i := 7;
while not Querytemp.Eof do
begin
//第一列写序列号
ExcelWorksheet1.Cells.item[i, 1] := inttostr(i-6);
//第二列写工号
ExcelWorksheet1.Cells.item[i, 2] := Querytemp.fieldbyname('smt_personnelid').Asstring;
//第三列写名字
ExcelWorksheet1.Cells.item[i, 3] := Querytemp.fieldbyname('smt_name').Asstring;
j:=5;
while j < DBGrid1.FieldCount+1 do
begin
//ExcelWorksheet1.Cells.item[i, 1] := ExcelWorksheet1.Rows.;
ExcelWorksheet1.Cells.item[i, j-1] :=Querytemp.fieldbyname(field_list[j-1]).Asstring;
inc(j)
end;
Querytemp.Next;
inc(i);
if i < Querytemp.RecordCount+6 then
Excel.ActiveSheet.Rows.Insert;
end;
Excel.Visible:=true;
Excel.Caption:='考勤月统计报表';
//打印预览
//ExcelWorksheet1.PrintPreview;
ExcelWorksheet1.SaveAs(SaveDialog1.FileName);
//导出
finally
{
Excel.ActiveWorkBook.Saved := True;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
Excel.quit;
}
Excel.ActiveWorkBook.Saved := True;
ExcelWorkbook1.Free;
ExcelWorksheet1.Free;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
Excel.quit;
end;
///########################################################################################
///########################################################################################
END;
END;
END;
end;
end;