2

I am trying to get the index of the last used row in a spreadsheet. I've found that in excel it could be done like that:

int lastUsedRow = worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,

Type.Missing).Row;

But this doesn't seem to work with GemBox. The idea is that I have a template excel file that I want to fill with more information and therefore need the last row, so that I can continue on the next one.

Apostrofix
  • 2,140
  • 8
  • 44
  • 71

2 Answers2

4

Hi you can just use ExcelFile.Rows.Count property.

Gets the number of currently allocated elements (dynamically changes when worksheet is modified)

Try the following:

int lastUsedRow = worksheet.Rows.Count - 1;

Also regarding the shahkalpesh suggestion, yes you can also achieve your task with that approach as well, here is how:

var usedRange = worksheet.GetUsedCellRange(true);
int lastUsedRow = usedRange.LastRowIndex;
GemBox Dev Team
  • 669
  • 5
  • 18
2

Note: I haven't used Gembox. My answer is based on searching in the documentation.

GetUsedCellRange returns a CellRange, which has a property named LastRowIndex.

Does this work the same way as Excel?

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
  • Thanks! I actually got here while searching for the last used column, not row. your answer is great – Alonzzo2 Jan 07 '16 at 13:41