A
austinjian
Unregistered / Unconfirmed
GUEST, unregistred user!
function TFormcg.DataSetToExcel(DS: TDataSet; MSExcel: OleVariant; eRange: OleVariant):Boolean;
var Count,I,Row: integer;
RangeName:string;
begin
with DS do
begin
if not Active then
begin
Result:=False;
WebApplication.ShowMessage('数据库没有打开');
Result:=False;
end;
end;
ds.first;
Result := True;
try
MyExcel:= CreateOleObject('Excel.Application');
except
WebApplication.ShowMessage('本机没有EXCEL,请安装 !');
Result:=False;
end;
MyExcel.Visible:= False;
MyExcel.WorkBooks.Add;
MyExcel.ScreenUpdating := False;
Count := DS.FieldCount-1;
eRange := MyExcel.Range['A1', 'A1']; //Excel文件的第一行
with DS do begin
for I := 0 to Count do begin
eRange.Value := Fields.DisplayLabel; //将字段头写入excel文件的第一行
eRange := eRange.Next; //Excel文件的下一列
end;
DisableControls;
First;
Row := 1;
while not eof do begin
Inc(Row); //row:=row+1; Excel文件的下一行
RangeName := 'A'+IntToStr(Row);
eRange := MyExcel.Range[RangeName, RangeName];
for I := 0 to Count do begin
eRange.Value := ''''+Fields.AsString;
eRange := eRange.Next;
end;
Next;
end;
EnableControls;
end;
MyExcel.ScreenUpdating := True;
MyExcel.Visible := True;
Result := True;
MyExcel.Quit;
end;
var Count,I,Row: integer;
RangeName:string;
begin
with DS do
begin
if not Active then
begin
Result:=False;
WebApplication.ShowMessage('数据库没有打开');
Result:=False;
end;
end;
ds.first;
Result := True;
try
MyExcel:= CreateOleObject('Excel.Application');
except
WebApplication.ShowMessage('本机没有EXCEL,请安装 !');
Result:=False;
end;
MyExcel.Visible:= False;
MyExcel.WorkBooks.Add;
MyExcel.ScreenUpdating := False;
Count := DS.FieldCount-1;
eRange := MyExcel.Range['A1', 'A1']; //Excel文件的第一行
with DS do begin
for I := 0 to Count do begin
eRange.Value := Fields.DisplayLabel; //将字段头写入excel文件的第一行
eRange := eRange.Next; //Excel文件的下一列
end;
DisableControls;
First;
Row := 1;
while not eof do begin
Inc(Row); //row:=row+1; Excel文件的下一行
RangeName := 'A'+IntToStr(Row);
eRange := MyExcel.Range[RangeName, RangeName];
for I := 0 to Count do begin
eRange.Value := ''''+Fields.AsString;
eRange := eRange.Next;
end;
Next;
end;
EnableControls;
end;
MyExcel.ScreenUpdating := True;
MyExcel.Visible := True;
Result := True;
MyExcel.Quit;
end;