Calling Cells.End in Excel

2020-07-19 03:28发布

I am trying to get the last Row in Column A. Does anyone know why the following does not compile?

It seem that it doesn't like .End

Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
xlWorkSheet = 
    (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

int iLastRow = xlWorkSheet.Cells[1,"A"]
    .End(Microsoft.Office.Interop.Excel.XlDirection.xlUp)
    .Row;

标签: c# excel
1条回答
SAY GOODBYE
2楼-- · 2020-07-19 03:43

It compiles for me, your method always returns 1, I did this and it worked:

Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = xlApp.Worksheets[1];
var iLastRow = xlWorkSheet.Range["A1","A65000"].get_End(XlDirection.xlDown).Row;

To find the last used row I think this method is better:

var usedRows = xlApp.WorksheetFunction.CountA(xlWorkSheet.Columns[1]);
查看更多
登录 后发表回答