不用这些用以下语句就能搞定:
var
e:variant;
i,j:integer;
colcount:integer;
columnwidth:array of integer;
begin
try
e:=CreateOleObject('Excel.Application');
e.workbooks.add();
e.visible:=true;
except
showmessage(' 请确认是否在本机上安装好MicroSoft Excel');
exit;
end; e.ActiveWindow.DisplayGridlines:=true;
colcount:=DBGrid1.Columns.Count ;
setlength(columnwidth,colcount);
for i:=low(columnwidth) to high(columnwidth) do
columnwidth[i]:=0;
e.Workbooks[1].Sheets[1].Cells[5,1] :=DBGrid1.Fields[0].DisplayName;
columnwidth[0]:=length(DBGrid1.Fields[0].DisplayName);
for i:=2 to ColCount do
begin
e.Workbooks[1].Sheets[1].Cells[5,i] :=DBGrid1.Fields[i-1].DisplayName;
columnwidth[i-1]:=length(DBGrid1.Fields[i-1].DisplayName);
end;
i:=1;
DBGrid1.DataSource.DataSet.First ;
while not DBGrid1.DataSource.DataSet.Eof do
begin
for j:=1 to ColCount do
begin
e.Workbooks[1].Sheets[1].Cells[5+i,j] := DBGrid1.Fields[j-1].AsString;
if columnwidth[j-1]<length(DBGrid1.Fields[j-1].AsString) then
columnwidth[j-1]:=length(DBGrid1.Fields[j-1].AsString);
end;
DBGrid1.DataSource.DataSet.Next;
i:=i+1;
end;
for i:=1 to colcount do
e.Workbooks[1].Sheets[1].Columns[i].ColumnWidth:=columnwidth[i-1];
别忘记买单呀
var
e:variant;
i,j:integer;
colcount:integer;
columnwidth:array of integer;
begin
try
e:=CreateOleObject('Excel.Application');
e.workbooks.add();
e.visible:=true;
except
showmessage(' 请确认是否在本机上安装好MicroSoft Excel');
exit;
end; e.ActiveWindow.DisplayGridlines:=true;
colcount:=DBGrid1.Columns.Count ;
setlength(columnwidth,colcount);
for i:=low(columnwidth) to high(columnwidth) do
columnwidth[i]:=0;
e.Workbooks[1].Sheets[1].Cells[5,1] :=DBGrid1.Fields[0].DisplayName;
columnwidth[0]:=length(DBGrid1.Fields[0].DisplayName);
for i:=2 to ColCount do
begin
e.Workbooks[1].Sheets[1].Cells[5,i] :=DBGrid1.Fields[i-1].DisplayName;
columnwidth[i-1]:=length(DBGrid1.Fields[i-1].DisplayName);
end;
i:=1;
DBGrid1.DataSource.DataSet.First ;
while not DBGrid1.DataSource.DataSet.Eof do
begin
for j:=1 to ColCount do
begin
e.Workbooks[1].Sheets[1].Cells[5+i,j] := DBGrid1.Fields[j-1].AsString;
if columnwidth[j-1]<length(DBGrid1.Fields[j-1].AsString) then
columnwidth[j-1]:=length(DBGrid1.Fields[j-1].AsString);
end;
DBGrid1.DataSource.DataSet.Next;
i:=i+1;
end;
for i:=1 to colcount do
e.Workbooks[1].Sheets[1].Columns[i].ColumnWidth:=columnwidth[i-1];
别忘记买单呀
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货