操作一个已存在Excel文件,里面有多个Sheet,利用Delphi打开这个Excel文件,在每个Sheet中的最后增加一行(每个Sheet行数不确定)Delphi里如何操作?谢谢!

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
        i,m:integer;
        excelapp,book,sheet:variant;
    begin
        excelapp:=createoleobject('excel.application');
        excelapp.visible:=false;
        excelapp.DisplayAlerts:=False;
        try        excelapp.workbooks.open('d:\123.xls');
            book:=excelapp.workbooks[1];        i:=book.worksheets.count;
            showmessage(inttostr(i));        for i:=1 to  book.worksheets.count do
            begin
                sheet:=excelapp.worksheets[i];
                m:=sheet.usedrange.rows.count;
                showmessage(inttostr(m));
                sheet.cells[m+1,1].value:='new new';
            end;        book.save;
            excelapp.quit;
        except
            excelapp.quit;
        end;
    end;