我要单击某一按钮调用以下过程
======================
procedure TransferGridToExcel(ATitle:Tstrings;Alv:TListView);
var
iCount,jCount,kCount:integer;
sTmp:string;
AVariant:Variant;
begin
try
AVariant:=GetActiveOleObject('Excel.Application');
except
try
AVariant:= CreateOleObject('Excel.Application');
except
showmessage('无法启动EXCEL程序。'+#13+'请确定该程序已正确安装!');
Exit;
end;
end; try
AVariant.visible:=false;
except
AVariant:= CreateOleObject('Excel.Application');
AVariant.visible:=false;
end;
AVariant.Workbooks.Add; //新建Excel文件
kCount :=1;
if ATitle <>nil then
begin
for iCount:=0 to ATitle.Count -1 do
begin
sTmp:=char(65+iCount)+inttostr(kCount);
AVariant.Range[sTmp].Select ; //选择单元
AVariant.ActiveCell.FormulaR1C1 :=CHR(255)+ATitle.Strings[iCount];
end;
kCount :=kCount+1 ;
end;
for iCount:=0 to Alv.Columns.Count -1 do
begin
sTmp:=char(65+iCount)+inttostr(kCount);
AVariant.Range[sTmp].Select ; //选择单元
AVariant.ActiveCell.FormulaR1C1 :=CHR(255)+alv.Column[iCount].caption ;
end;
kCount:=kCount+1;
for jCount :=0 to alv.Items.Count -1 do
begin
sTmp:='A'+inttostr(jCount+kCount);
AVariant.Range[sTmp].Select ; //选择单元
AVariant.ActiveCell.FormulaR1C1 := char(255)+alv.Items.Item[jCount].Caption ; //对所选择的单元赋值
for iCount:=0 to Alv.Items.Item[jCount].SubItems.Count-1 do
begin
sTmp:=char(65+iCount+1)+inttostr(jCount+kCount);
AVariant.Range[sTmp].Select ; //选择单元
AVariant.ActiveCell.FormulaR1C1 :=CHR(255)+Alv.Items.Item[jCount].SubItems.Strings[iCount] ; //对所选择的单元赋值
end;
end;
AVariant.Range['A1'].Select ;
AVariant.visible:=true;
AVariant:=Unassigned;
end;
======================
procedure TransferGridToExcel(ATitle:Tstrings;Alv:TListView);
var
iCount,jCount,kCount:integer;
sTmp:string;
AVariant:Variant;
begin
try
AVariant:=GetActiveOleObject('Excel.Application');
except
try
AVariant:= CreateOleObject('Excel.Application');
except
showmessage('无法启动EXCEL程序。'+#13+'请确定该程序已正确安装!');
Exit;
end;
end; try
AVariant.visible:=false;
except
AVariant:= CreateOleObject('Excel.Application');
AVariant.visible:=false;
end;
AVariant.Workbooks.Add; //新建Excel文件
kCount :=1;
if ATitle <>nil then
begin
for iCount:=0 to ATitle.Count -1 do
begin
sTmp:=char(65+iCount)+inttostr(kCount);
AVariant.Range[sTmp].Select ; //选择单元
AVariant.ActiveCell.FormulaR1C1 :=CHR(255)+ATitle.Strings[iCount];
end;
kCount :=kCount+1 ;
end;
for iCount:=0 to Alv.Columns.Count -1 do
begin
sTmp:=char(65+iCount)+inttostr(kCount);
AVariant.Range[sTmp].Select ; //选择单元
AVariant.ActiveCell.FormulaR1C1 :=CHR(255)+alv.Column[iCount].caption ;
end;
kCount:=kCount+1;
for jCount :=0 to alv.Items.Count -1 do
begin
sTmp:='A'+inttostr(jCount+kCount);
AVariant.Range[sTmp].Select ; //选择单元
AVariant.ActiveCell.FormulaR1C1 := char(255)+alv.Items.Item[jCount].Caption ; //对所选择的单元赋值
for iCount:=0 to Alv.Items.Item[jCount].SubItems.Count-1 do
begin
sTmp:=char(65+iCount+1)+inttostr(jCount+kCount);
AVariant.Range[sTmp].Select ; //选择单元
AVariant.ActiveCell.FormulaR1C1 :=CHR(255)+Alv.Items.Item[jCount].SubItems.Strings[iCount] ; //对所选择的单元赋值
end;
end;
AVariant.Range['A1'].Select ;
AVariant.visible:=true;
AVariant:=Unassigned;
end;
var
aa:Tstrings;
begin
aa:=tstringlist.Create;
aa.Clear;
aa.add('1');
aa.add('2');
TransferGridToExcel(aa,self.ListView1);
aa.free;
end;
var
List:TStrings;//第一个参数
begin
TransferGridToExcel(List,ListView1)//ListView1一个ListView控件
end;
如果不在同一個窗體上,要公共定義一下,調用的時候加上FORMX.TransferGridToExcel