NPOI icon indicating copy to clipboard operation
NPOI copied to clipboard

row.LastCellNum is to high

Open danny-p93 opened this issue 5 years ago • 2 comments

Hello there, i recently started working with NPOI and just did some experiments to get to know the Framework. So i created a very simple xlsx file and looked how to get acces to the data.

Here is my code:

var sheet = WB.GetSheetAt(0);
int check = sheet.LastRowNum;
var r = sheet.GetRow(3);
var sb = new StringBuilder();
var cell = r.GetCell(4);
sb.AppendLine(string.Format("row  from {0} to {1}", sheet.FirstRowNum, sheet.LastRowNum));
sb.AppendLine(string.Format("Col from {0} to {1}", r.FirstCellNum, r.LastCellNum));

So nothing difficult. with the following example file i expected row from 0 to 5 col from 0 to 4 HelloWorld.xlsx

But actually LastCellNum says 5. And when i looked into r.Cells (the array has the 4 expected cells) and jumped to the last cell it says ColumnIndex = 4.

At first i thought there is just an empty Cell in the next column but after i deleted the complete coloumn with ctrl+- the result was the same. And the number changed aswell as i added the last column with the random character.

danny-p93 avatar Aug 29 '18 11:08 danny-p93

Aha, i'm also curious about this question. The 'CellNum' starts from 0 and ends with the actual max number plus 1. In my opinion it's a mistake which cannot be corrected because a lot of people realize and obey it. If it changes, it may cause a large disaster for the projects using it. The developers cannot inform total users of the changes which aren't compatible to old versions.

LeaFrock avatar Sep 07 '18 07:09 LeaFrock

Firstly, blank cells don't mean it's really empty in some cases. The blank cells look same as non-existing cells. For any case, while loop through the cells, don't forget to check if the cell is null and if you go over the upper boundary of Cells property.

tonyqus avatar Nov 22 '18 22:11 tonyqus