3

I'm using POI java library to read an Excel.

My Exel have a simple structure composed by 8 columns.

The problem is that reading column length by method getPhysicalNumberOfCells I get different number for each row.

zeppaman
  • 844
  • 8
  • 23
  • possible duplicate of [get number of columns of a particular row in given excel](http://stackoverflow.com/questions/18489817/get-number-of-columns-of-a-particular-row-in-given-excel) – Sankumarsingh Apr 30 '14 at 05:50

1 Answers1

7

The problem is that getPhysicalNumberOfCells has a different meaning of what I was thinking.

getPhysicalNumberOfCells returns the number of cell in a row that have a content.

In fact POI stores for each row only the data added into exel, for example if you have data in columns 0,3,5, getPhysicalNumberOfCells will return always 3, beacause 3 is the number of "filled" cells.

To achive the purpose of get the logical numeber of cell in a row we use:

getLastCellNum() 

According whit documentation, this method gets the index of the last cell. This value is increased BY ONE, so in the example above, whit maximum index of 5 will be 6.. I think this has been done to simplify iteration over rowcell.

Moreover, there is a method

getLastCellNum

that show first cell index.

An example inspired from official documentation:

  short minColIdx = row.getFirstCellNum();
  short maxColIdx = row.getLastCellNum();
  for(short colIdx=minColIdx ; colIx<maxColIdx ; colIdx++) {
     Cell cell = row.getCell(colIx);
    //get value of cell

}

zeppaman
  • 844
  • 8
  • 23
  • 1
    Wow, according to what I see, you put yourself your answer the exact same moment you posted your question. – slaadvak Apr 29 '14 at 17:09
  • 2
    http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – zeppaman Apr 29 '14 at 17:41
  • This can be done only if you think what you know, other don't know... but we all know the difference between the two you are saying... once you have done the same... please accept the answer so that it cannot be taken as unresolved problem. – Sankumarsingh Apr 30 '14 at 05:46
  • http://stackoverflow.com/a/18489900/624003, http://stackoverflow.com/q/18201381/624003 – Sankumarsingh Apr 30 '14 at 05:51
  • There will be no complete iteration if sheet contain empty cell though value of last cell is fine. – Sajjad Ali Oct 02 '15 at 06:52