查找工作表上最后使用的单元格并将行复制到另一个工作表?

Finding the last used cell on worksheet and copying row into another worksheet?

如有任何答复,我们将不胜感激。 我的问题如下:

这是确定我的 excel 作品sheet 的最后一个非空 cell/row 的正确方法吗?

xl.Worksheet sh;
        int lastRow;
        int fullRow;

        sh = xlApp.Workbooks.get_Item("myExcelFile").Worksheets.get_Item("MySheet");
        fullRow = sh.Rows.Count;
        lastRow = sh.Cells[fullRow, 1].End(xl.XlDirection.xlUp).Row;

如果这确实是正确的,我该如何将我的行复制到另一个作品中sheet。更具体地说,A 列的第一个空单元格。 我知道我必须使用这样的命令:

lastRow.EntireRow.Copy(FirstEmptyCell);

作为一个 int 类型,我无法将它复制到我的另一个 sheet。 有什么建议吗?

lastRow = sh.Cells[fullRow, 1].End(xl.XlDirection.xlUp).Row;

此处,lastRow 是一个 int,因此您不能将其用作下一行的对象:

lastRow.EntireRow.Copy(FirstEmptyCell); // int doesn't have an EntireRow property

可以做的是将两者合并成一行:

Excel.Range myLastRow;
myLastRow = sh.Cells[sh.Rows.Count, 1].End(xl.xlDirection.Up).EntireRow;

现在 myLastRow 是您要复制的整行。

myLastRow.Copy(destination);