我给你一个通用的程序,他自动显示DBGrid所所连接的Table的的内容。别忘了给我加分。 在 interface 中加 uses leServer, Excel97 在 implementation 中加 uses ComObj; Procedure TBRForm.ExcelActionExecute(Sender: TObject); var i,j: Integer; sheet: Variant; XLApp: Variant; begin XLApp:= CreateOleObject('Excel.Application'); XLApp.Visible:= True; XLApp.WorkBooks.Add(xlWBatWorkSheet); XLApp.WorkBooks[1].WorkSheets[1].Name:='进销存'; sheet:= XLApp.Workbooks[1].WorkSheets['进销存']; with DBGrid1.DataSource.DataSet do begin for i:= 0 to FieldCount-1 do sheet.Cells[1,i+1]:= Fields[i].DisplayLabel; j:=1; First; while not Eof do begin j:=j+1; for i:= 0 to FieldCount-1 do sheet.Cells[j,i+1]:= Fields[i].AsString; Next; end; end; end;
v:Variant;
begin
v:=CreateOleObject('Excel.Application');
v.Visible:=False;
......
end;
对V的操作和对Delphi下一个控件的操作一样,至于Excel的属性,方法你看看VBA就行了。
在 interface 中加
uses
leServer, Excel97
在 implementation 中加
uses ComObj;
Procedure TBRForm.ExcelActionExecute(Sender: TObject);
var
i,j: Integer;
sheet: Variant;
XLApp: Variant;
begin
XLApp:= CreateOleObject('Excel.Application');
XLApp.Visible:= True;
XLApp.WorkBooks.Add(xlWBatWorkSheet);
XLApp.WorkBooks[1].WorkSheets[1].Name:='进销存';
sheet:= XLApp.Workbooks[1].WorkSheets['进销存']; with DBGrid1.DataSource.DataSet do
begin
for i:= 0 to FieldCount-1 do sheet.Cells[1,i+1]:= Fields[i].DisplayLabel;
j:=1;
First;
while not Eof do
begin
j:=j+1;
for i:= 0 to FieldCount-1 do
sheet.Cells[j,i+1]:= Fields[i].AsString;
Next;
end;
end;
end;
我不知道里面的一个参数。