How to know the number of rows an xlsx file has in c#?

I am occupying this code to get the first cell of the file

Excel.Application excelApp = new Excel.Application();
        excelApp.Visible = true;
        string workbookPath = "c:/Users/Edgar/Desktop/base/Libro1.xlsx";//‪C:\Users\Edgar\Desktop\base\Libro1.xlsx
        Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, true, 0, true, false, false);
        Excel.Sheets excelSheets = excelWorkbook.Worksheets;
        excelApp.Visible = false;
        string currentSheet = "Hoja1";
        Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);







        Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");
        string str = (excelCell.Cells as Excel.Range).Value2;

        MessageBox.Show(str);

But I want to save in a variable the number of rows that are filled in xlsx

 1
Author: Marc Lemien, 2016-07-21

1 answers

I understand that you did not follow the advice of the other question

¿working with an xlsx file without opening excel in C#?

Where we recommend using some open xml based library

>>I want to save in a variable the number of rows that are filled in xlsx

You could use

Worksheet.UsedRange Property

Then you would do

Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);

Excel.Range range = xlWorkSheet.UsedRange;

int rCnt = range.Rows.Count;
 2
Author: Leandro Tuttini, 2017-04-13 13:00:52