To wt741963,
我的QQ:82780254
procedure ExportExcel(Title: String;
Handle: Thandle;
Qry: TADOQuery;
DB: TDBGridEh;
Flag: Integer);
var
Excel: Variant;
WorkBook: OleVariant;
WorkSheet: OleVariant;
I, J: Integer;
begin
try
if Qry.RecordCount = 0 then
begin
MessageBox(Handle, Mess98002, MessTitleCHN,
MB_ICONINFORMATION + MB_OK);
Exit;
end;
Excel := CreateOleObject('Excel.Application');
Excel.Application.Workbooks.add;
Excel.Caption := Title;
Excel.Application.Visible := True;
WorkBook := Excel.Application.WorkBooks[1];
WorkSheet := WorkBook.WorkSheets.Item[1];
except
MessageBox(Handle, Mess98003, MessTitleCHN,
MB_ICONINFORMATION + MB_OK);
Exit;
end;
case Flag of
1:
begin
I := 1;
Qry.First;
for J := 0 to DB.FieldCount - 1do
begin
WorkSheet.Cells[1, J + 1].HorizontalAlignment := 3;
WorkSheet.Cells[1, J + 1] := DB.Fields[J].DisplayName;
end;
while not Qry.Eofdo
begin
Inc(I);
for J := 0 to DB.FieldCount - 1do
begin
if (J = 0) or (J = 1) then
WorkSheet.Cells[I, J + 1].HorizontalAlignment := 2
else
WorkSheet.Cells[I, J + 1].NumberFormat := '0.00';
WorkSheet.Cells[I, J + 1] := DB.Fields[J].AsString;
end;
Qry.Next;
end;
end;
2:
begin
I := 1;
Qry.First;
for J := 0 to DB.FieldCount - 1do
begin
WorkSheet.Cells[1, J + 1].HorizontalAlignment := 3;
WorkSheet.Cells[1, J + 1] := DB.Fields[J].DisplayName;
end;
while not Qry.Eofdo
begin
Inc(I);
for J := 0 to DB.FieldCount - 1do
begin
if (J = 0) or (J = 1) or (J = 8) then
WorkSheet.Cells[I, J + 1].HorizontalAlignment := 2
else
begin
WorkSheet.Cells[I, J + 1].NumberFormat := 'hh:mm';
with FlagColordo
begin
case Qry.FieldByName('Flag' + IntToFlag(J - 1)).AsInteger of
FlagHols:
WorkSheet.Cells[I, J + 1].Interior.Color := ColorHols;
FlagEvection:
WorkSheet.Cells[I, J + 1].Interior.Color := ColorEvection;
FlagOvertime:
WorkSheet.Cells[I, J + 1].Interior.Color := ColorOvertime;
FlagAbsent:
WorkSheet.Cells[I, J + 1].Interior.Color := ColorAbsent;
FlagLate:
WorkSheet.Cells[I, J + 1].Interior.Color := ColorLate;
FlagEarly:
WorkSheet.Cells[I, J + 1].Interior.Color := ColorEarly;
FlagForget:
WorkSheet.Cells[I, J + 1].Interior.Color := ColorForget;
end;
end;
end;
if J = 8 then
begin
if Qry.FieldByName('IsRest').AsBoolean = True then
WorkSheet.Cells[I, J + 1] := '√'
else
WorkSheet.Cells[I, J + 1] := 'X';
WorkSheet.Cells[I, J + 1].HorizontalAlignment := 3;
end
else
WorkSheet.Cells[I, J + 1] := DB.Fields[J].AsString;
end;
Qry.Next;
end;
end;
3:
begin
I := 1;
Qry.First;
for J := 0 to DB.FieldCount - 1do
begin
WorkSheet.Cells[1, J + 1].HorizontalAlignment := 3;
WorkSheet.Cells[1, J + 1] := DB.Fields[J].DisplayName;
end;
while not Qry.Eofdo
begin
Inc(I);
for J := 0 to DB.FieldCount - 1do
begin
if (J < 2) then
WorkSheet.Cells[I, J + 1].HorizontalAlignment := 2;
if (J > 1) and (J < 14) then
WorkSheet.Cells[I, J + 1].NumberFormat := '0.00';
WorkSheet.Cells[I, J + 1] := DB.Fields[J].AsString;
end;
Qry.Next;
end;
end;
4:
begin
I := 1;
Qry.First;
for J := 0 to DB.FieldCount - 1do
begin
WorkSheet.Cells[1, J + 1].HorizontalAlignment := 3;
WorkSheet.Cells[1, J + 1] := DB.Fields[J].DisplayName;
end;
while not Qry.Eofdo
begin
Inc(I);
for J := 0 to DB.FieldCount - 1do
begin
WorkSheet.Cells[I, J + 1].HorizontalAlignment := 2;
WorkSheet.Cells[I, J + 1] := DB.Fields[J].AsString;
end;
Qry.Next;
end;
end;
5:
begin
I := 1;
Qry.First;
for J := 0 to DB.FieldCount - 1do
begin
WorkSheet.Cells[1, J + 1].HorizontalAlignment := 3;
WorkSheet.Cells[1, J + 1] := DB.Fields[J].DisplayName;
end;
while not Qry.Eofdo
begin
Inc(I);
for J := 0 to DB.FieldCount - 1do
begin
if (J = 0) or (J = 8) then
WorkSheet.Cells[I, J + 1].HorizontalAlignment := 2;
WorkSheet.Cells[I, J + 1] := DB.Fields[J].AsString;
if J = 8 then
WorkSheet.Cells[I, J + 1].NumberFormat := '#';
if (J = 9) or (J = 10) then
WorkSheet.Cells[I, J + 1].NumberFormat := 'yyyy-mm-dd';
end;
Qry.Next;
end;
end;
end;
end;