Delphi实现操作Excel示例

简单实现读写Excel,用C#也可以实现,不过没有Delphi方便简单。

implementation
uses ComOBJ;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var ExcelAPP1:Variant;
    ExcelApp2:Variant;
    I,j: Integer;
    ID,IDX:string;
begin
try
  ExcelAPP1:=CreateOleObject('Excel.Application');
  ExcelAPP1.Workbooks.Open('D:\Program Files\Embarcadero\Projects\操作Excel\奖.xls');
  ExcelAPP2:=CreateOleObject('Excel.Application');
  ExcelApp2.Workbooks.Open('D:\Program Files\Embarcadero\Projects\操作Excel\贫2.xls');
  for I := 2 to 610 do
  begin
    Label1.Caption:=IntToStr(i)+'行';
    ID:=trim(ExcelAPP1.cells[I,2].value);
    for j := 4 to 549 do
    begin
      IDX:=trim(ExcelAPP2.cells[j,4].value);
      if (IDX=ID) then
      begin
        ExcelAPP1.cells[i,12].value:='已建档';
        Break;
      end;
    end;
  end;
  Label1.Caption:='全部完成,正在保存......';
  ExcelAPP1.Save;
  Label1.Caption:='全部完成,已保存';
finally
  ExcelAPP1.WorkBooks.Close;
  ExcelAPP1.Quit;
  ExcelApp2.WorkBooks.Close;
  ExcelAPP2.Quit;
end;
end;