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 ErrMsg('无法启动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
我试过不行吧
qinmaofan(采菊南山下 <抵制日货 从我做起>) ;有沒有現成的?:)
var
iCount,jCount,kCount:integer;
sTmp:string;
AVariant:Variant;
begin
try
AVariant:=GetActiveOleObject('Excel.Application');
except
try
AVariant:= CreateOleObject('Excel.Application');
except
ErrMsg('无法启动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