主页 > 编程资料 > Delphi >
发布时间:2015-09-22 作者:网络 阅读:73次

procedure TForm1.Button1Click(Sender: TObject);
var
Data : TDataSet;
ADBGrid: TDBGrid;
i, j : integer;
ExcelApp:Variant;

begin
DBGrid1.Enabled:=false;
ExcelApp := CreateOleObject('Excel.Application');


ExcelApp.WorkBooks.add;
ExcelApp.Columns[5].ColumnWidth:=20;
ExcelApp.Columns[6].ColumnWidth:=20;
ADBGrid := DBGrid1;
Data := ADBGrid.DataSource.DataSet;
with ADBGrid do { Insert Table Header }
for i := 1 to Columns.Count do
if Columns[i - 1].Visible then
ExcelApp.Cells[1,i].Value :=Columns[i - 1].Title.Caption;

Data.First;
i:=2;
while not Data.Eof do { Process All record }
begin
with ADBGrid do { Process one record }
for j := 1 to Columns.Count do
if Columns[j - 1].Visible then
ExcelApp.Cells[i,j].Value := Columns[j - 1].Field.DisplayText;
Inc(i);
Data.Next;
end;
ExcelApp.Visible:=true;
DBGrid1.Enabled:=true;
end;

uses comobj;

关键字词: