给你看看这个:
procedure TfrmReportLib.ExportPersonMove;
const ColField:array[2..7] of string=('sumdate','cnttotal','cntenter','cntleave','cntin','cntout');
var
Excel,WorkBook, Sheet:variant;
qryDept:TADOQuery;
Row,Col,iPos:Integer;
sDept,s1,sDateFrom,sDateTo:string;
tempdate:TDatetime;
begin
if not trystrtodate(edYdFrom.Text,tempdate) then
begin
//showmessage('错误日期!');
dmmaindata.ShowMesStr('错误日期!','030');
edYdFrom.SetFocus;
exit;
end;
sDateFrom:=Copy(edYdFrom.Text,1,7);
if not trystrtodate(edYdTo.Text,tempdate) then
begin
//showmessage('错误日期!');
dmmaindata.ShowMesStr('错误日期!','030');
edYdTo.SetFocus;
exit;
end;
sDateTo:=Copy(edYdTo.Text,1,7);
CreateExcelApp(Excel,WorkBook,Sheet);
Sheet.Name:='人员异动汇总表';
Row:=1;
Sheet.Range['C1:F1'].Select;
Excel.Selection.HorizontalAlignment:=$FFFFEFF4;
Excel.Selection.MergeCells:=True;
Sheet.Range['C1:F1'].Select;
Excel.ActiveCell.FormulaR1C1:='部门人员异动汇总表';
Excel.ActiveCell.Font.Bold := True;
Excel.ActiveCell.Font.Size:=14;
Row:=2;
Sheet.Range['A2:F2'].Select;
Excel.Selection.HorizontalAlignment:=$FFFFEFDD;
Excel.Selection.MergeCells:=True;
Sheet.Range['A2:F2'].Select;
Excel.ActiveCell.FormulaR1C1:='月份: '+sDateFrom+' 到 '+sDateTo;
Excel.ActiveCell.Font.Size:=12;
Excel.ActiveCell.Font.ColorIndex:=3;
Row:=3;
Sheet.Rows[Row].Font.Bold := True;
Sheet.Rows[Row].Font.Size:=12;
Sheet.Rows[Row].HorizontalAlignment:=$FFFFEFF4;
Sheet.Cells[Row,1]:='部门';
Sheet.Cells[Row,2]:='月份';
Sheet.Cells[Row,3]:='当月人数';
Sheet.Cells[Row,4]:='新进';
Sheet.Cells[Row,5]:='离职';
Sheet.Cells[Row,6]:='调入';
Sheet.Cells[Row,7]:='调出';
qryDept:=TADOQuery.Create(nil);
try
with qryDeptdo
begin
Connection:=dmMainData.MainConnection;
iPos:=Pos('[',cbBranchno.Text);
s1:=Copy(cbBranchno.Text,1,iPos-1);
SQL.Add('exec spPersonMoveSum '+QuotedStr(sDateFrom)+','+QuotedStr(sDateTo)+','+QuotedStr(s1));
Open;
if recordcount<=0 then
Exit;
First;
sDept:='';
repeat
Row:=Row+1;
s1:=FieldByName('branchName').AsString+'('+FieldByName('branchno').AsString+')';
if sDept<>s1 then
begin
sDept:=s1;
Sheet.Cells[Row,1]:=sDept;
Sheet.Cells[Row,1].Font.ColorIndex:=3;
Sheet.Cells[Row,1].HorizontalAlignment:=$FFFFEFF4;
end;
for col:=2 to 7do
begin
Sheet.Cells[Row,Col]:=FieldByName(ColField[Col]).AsString;
Sheet.Cells[Row,Col].HorizontalAlignment:=$FFFFEFC8;
end;
Next;
until Eof;
end;
finally
Sheet.Cells.Columns.AutoFit;
Sheet.Columns[1].HorizontalAlignment:=$FFFFEFF4;
Sheet.Range['A4'].Select;
Excel.ActiveWindow.FreezePanes:=True;
Excel.Visible:=True;
qryDept.Free;
end;
end;
procedure TfrmReportLib.CreateExcelApp(var Excel, WorkBook,
Sheet: variant);
begin
try
Excel:= Unassigned;
Excel:=GetActiveOleObject('Excel.Application');
except
Try //调用Excel
Excel:=UnAssigned;
Excel:=CreateOleObject('Excel.Application');
Excel.Visible:=True;
Excel.DisplayAlerts:=false;
Excel.WorkBooks.Add;
Except
//raise exception.Create('请检查是否正确安装了Excel!');
dmmaindata.ShowMesStr('请检查是否正确安装了Excel!','030');
Excel:=UnAssigned;
Exit;
end;
end;
Excel.SheetsInNewWorkbook := 1;
WorkBook := Excel.WorkBooks.Add;
Sheet := WorkBook.WorkSheets[1];
Sheet.Cells.VerticalAlignment := $FFFFEFF4;
end;