0

I am using the following code to determine the last row of the Excel file before I start reading. If column1 values are empty then it does not work. Is there a way to check all rows or row 2

            //Count number of rows
            range = (Excel.Range)xlWorkSheet.Cells[xlWorkSheet.Rows.Count, 1];

            //Get directoryname
            string directoryname = Path.GetDirectoryName(edifile);

            //Remove special characters
            string filenameNoExt = RemoveSpecialCharacters(Path.GetFileNameWithoutExtension(edifile));

            //Build Unique Guid Filename
            csvfile = path + filenameNoExt + "_" + Utilities.GetMyGUID();


            //xlUp will move up the worksheet to find the last cell with data.
            long lastRow = (long)range.get_End(Excel.XlDirection.xlUp).Row;
Tremor
  • 9
  • 1
  • 4
  • Sorry. It is C#. – Tremor Mar 07 '18 at 19:39
  • What if you did a loop to check if `lastRow = 1`. If it's equal to 1, then set `range` to column 2 and try again, etc.? Perhaps a `while lastRow = 1` loop or something? – BruceWayne Mar 07 '18 at 19:51
  • [This](https://stackoverflow.com/questions/1284388/how-to-get-the-range-of-occupied-cells-in-excel-sheet) could help – cybernetic.nomad Mar 07 '18 at 20:01
  • var rowCount = worksheet .Dimension.End.Row ? https://stackoverflow.com/questions/18733810/programmatically-getting-the-total-no-of-rows-having-data-in-excel-sheet-using – QHarr Mar 07 '18 at 20:04
  • Thanks BruceWayne. I was able to get your suggestion to work. – Tremor Mar 08 '18 at 18:10

0 Answers0