这是我修改DFW离线资料的例子,用的OLE,在 D6+IW5.053+Office 2K 下通过:
没有用IWGrid,用的是与IWGrid相连数据集DataSet 。
uses ....DB,ComObj,....
...
private
function DataSetToExcel(DS: TDataSet; MSExcel: OleVariant; eRange: OleVariant):Boolean;
...
implementation
function TformBookInfoEdit.DataSetToExcel(DS: TDataSet; MyExcel: 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;
Abort;
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;
procedure TformBookInfoEdit.IWButton1Click(Sender: TObject);
var MyExcel,eRange:Variant;
begin
if DataSetToExcel(IWGrid.DataSource.DataSet,MyExcel, eRange) then
WebApplication.ShowMessage('保存成功');
end;