title: Apache POI reliable way to determine last row
body: <p>I'm using Apache POI to read data from an Excel spreadsheet and sending a POST to a remote service. </p>
  
  <p>While testing my implementation I found out it blew up with an exception when my method tried to parse an empty string into a BigDecimal.
  I discovered this is due to the fact that, according to the Apache POI documentation, getLastRowNum() may </p>
  
  <blockquote>
    <p>rows which had content before and were set to empty later might still be counted as rows by Excel and Apache POI, so the result of this method will include such rows and thus the returned value might be higher than expected!</p>
  </blockquote>
  
  <p>And lo and behold, it did return a bigger number than I had initially anticipated, forcing me to use the value in the first cell in the row as the exit condition for my loop, something like the following:</p>
  
  <pre class="lang-java prettyprint-override"><code>String productCode= excelMapper.getValueFromRow(r, "product_code");
  
  while (productCode!= null &amp;&amp; !productCode.isEmpty()) {
      MyBeanClass bean = new MyBeanClass ();
      r = sheet.getRow(i);
  
      productCode = excelMapper.getValueFromRow(r, "cod_prodotto");
      yada yada....
  }
  </code></pre>
  
  <p>This works for my purposes, but I was wondering if there was a better way.</p>