NPOI
NPOI copied to clipboard
row.LastCellNum is to high
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.
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.
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.