本文整理自网络,侵删。
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;
相关阅读 >>
Delphi firemonkey处理图形的方式与vcl处理图形的方式大不相同
老外写的在桌面添加快捷方式(Delphi xe5 android)
Delphi获取flash文件的影片时长,原始尺寸,帧数等信息
更多相关阅读请进入《Delphi》频道 >>