Thursday, April 20, 2023

get excel rows count c#

public int GetRowsCountFromExcel(string fileName, string downloadPath = @"C:\Downloads") 
        {
            /* //Read the downloaded Excel file and count the rows
             string filePath = Path.Combine(downloadPath, fileName);
             ExcelPackage excelPackage = new ExcelPackage(new FileInfo(filePath));
             ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[0];
             int rowCount = worksheet.Dimension.Rows;       
*/
            string filePath = Path.Combine(downloadPath, fileName);
            XSSFWorkbook workBook = new XSSFWorkbook(File.Open(filePath, FileMode.Open));
            var sheet = workBook.GetSheetAt(0);
            int count = sheet.LastRowNum + 1;
            //Clean up: delete the downloaded file
            //File.Delete(filePath);
            return count;
        }

No comments:

Post a Comment