以下代码供参考:
uses
ComObj;
type
TForm1 = class(TForm)
Label1: TLabel;
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
myrec:TSearchRec;
begin
if findfirst(ExtractFilePath(Application.ExeName)+'DATA/*.XLS',faAnyFile,myrec)=0 then
listbox1.Items.Add(myrec.name);
while findnext(myrec)=0do
listbox1.Items.Add(myrec.name);
findclose(myrec);
if listbox1.Items.Count> 0 then
label1.Caption:='合计发现 EXCEL 文件 '+inttostr(listbox1.items.count)+' 个。';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
formexcel,toexcel:Variant;
s:string;
i,j,k,l:integer;
begin
toexcel:=CreateOleObject('Excel.Application');
toexcel.WorkBooks.open(ExtractFilePath(Application.ExeName)+'WORK/tmp.xls');
toExcel.Visible:=false;
j:=1;
for i:=listbox1.Items.Count-1do
wnto 0do
begin
formexcel:=CreateOleObject('Excel.Application');
formexcel.WorkBooks.Open(ExtractFilePath(Application.ExeName)+'DATA/'+listbox1.Items);
formExcel.Visible:=false;
k:=1;
while truedo
begin
for l:=1 to 20do
if formexcel.cells[1,l].value='实发合计' then
break;
s:=formExcel.Cells[k,1].Value;
if (s='') or (s='合 计') then
break;
if length(s)<>5 then
k:=k+1
else
begin
toexcel.cells[j,1].value:=formexcel.cells[k,1].value;
toexcel.cells[j,2].value:=formexcel.cells[k,2].value;
toexcel.cells[j,3].value:=formexcel.cells[k,l].value;
j:=j+1;
k:=k+1;
end;
end;
formexcel.quit;
listbox1.Items.Delete(i);
end;
label1.Caption:='完成全部EXCEL文件的合并工作。';
Button1.Enabled:=false;
toexcel.displayalerts:=false;
toexcel.save;
toexcel.Quit;
end;