title: Get Excel Information with Java
body: <p>I need some help here as I am stuck...</p>
  
  <p>Well the background is this...</p>
  
  <p>I am making a flex application and one service of this is, to read an excel file and put it into a database...</p>
  
  <p>I do know how to retrieve the info but there are some details that got me stuck...</p>
  
  <p>Here there are:</p>
  
  <ol>
  <li>Some excel files might have like 1600 or more rows BUT only 100 or 200 have data...BUT I want to know if there are like a cell.getlastrow function BUT WITH DATA because <code>System.out.println(sheet.getLastRowNum());</code> gave me obviously 1600 or whatever it is, but doesnt gave me the last row with data also in my insert validation  I did  <code>&amp;&amp; (cell.getStringCellValue() != null)</code>  in my condition but didn't work ... and I want to stop at the last row because if not then my code  continue reading and it take lot of time of something that it supposed not to do and insert something I don't want to. :(</li>
  <li>Also, some of the fields are like this " 7 " , "0" I mean Numbers BUT if I put them as getStringCellValue, it doesn't put them! and if I put them as getNumericCellValue it puts them but like "7.0" and stuff like that, how can I get it as String? I want them as String because in my database I need them like integers not floats or doubles</li>
  <li>And another question, the excel files might be xls or xlsx I read that its better to make <code>Workbook eworkbook = WorkbookFactory.create(Archivo);</code> instead of the instances of new XSSF or HSSF etc ... with WorkbookFactory, will I be safe whether is xls or xlsx?</li>
  </ol>
  
  <p>For better comprehension I'll put some of the code where I do that...</p>
  
  <pre><code>SiveCuatro element;
  
      ByteArrayInputStream Archivo = new ByteArrayInputStream( params.byteArray );
  
      DsMgr myDB = new DsMgr();
      Connection con = myDB.getConnection();
  
      if (con != null){
          Statement stmt;
  
          try{
  
              stmt = con.createStatement();
              Workbook eworkbook = WorkbookFactory.create(Archivo);
              Sheet sheet = eworkbook.getSheet("FORMATO SIVE 04");
  
              Iterator&lt;Row&gt; rowIterator = sheet.iterator();
              element = new SiveCuatro(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
              while(rowIterator.hasNext()) {
                  Row row = rowIterator.next();
  
                  Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
                  while(cellIterator.hasNext()) {
  
                      Cell cell = cellIterator.next();
  
  
                      int NumRows = sheet.getPhysicalNumberOfRows();
                      int pasa = NumRows + 1;
  
  
                      switch(cell.getCellType()) {
                      case Cell.CELL_TYPE_STRING:
                          if ( (cell.getColumnIndex() &gt;= 0) &amp;&amp; (pasa &gt;= 1 ) ){
                              if(cell.getColumnIndex() == 1){element.setLaboratorio_Id(cell.getStringCellValue());}
                              if(cell.getColumnIndex() == 2){element.setCaso_IdLab(cell.getStringCellValue());}
                              if(cell.getColumnIndex() == 3){element.setEstado_Id(cell.getStringCellValue());}
                              if(cell.getColumnIndex() == 4){element.setMunicipio_Id(cell.getStringCellValue());}
                              if(cell.getColumnIndex() == 5){element.setEnfermedad_Id(cell.getStringCellValue());}
                              if(cell.getColumnIndex() == 6){element.setEspecie_Id(cell.getStringCellValue());}
                              if(cell.getColumnIndex() == 10){element.setTipoMuestra_Id(cell.getStringCellValue());}
                              if(cell.getColumnIndex() == 18)element.setTecnica_Id(cell.getStringCellValue());
                              if(cell.getColumnIndex() == 19)element.setUsuario_Id(cell.getStringCellValue());
                              if(cell.getColumnIndex() == 21)element.setCaso_Fecha(cell.getStringCellValue());
                              if(cell.getColumnIndex() == 22)element.setCaso_Anio(cell.getStringCellValue());
                              if(cell.getColumnIndex() == 29)element.setPropietario(cell.getStringCellValue());
                              if(cell.getColumnIndex() == 30)element.setGranjaPredio(cell.getStringCellValue());
                              if(cell.getColumnIndex() == 31)element.setFuncionZoote(cell.getStringCellValue());
                              if((cell.getStringCellValue() != null)){ break;}
                          }
                          break;
                      case Cell.CELL_TYPE_NUMERIC:
                          if ( (cell.getColumnIndex() &gt;= 0) &amp;&amp; (pasa &gt;= 1 )  ){
                              if(cell.getColumnIndex() == 0){element.setCaso_Mes(cell.getNumericCellValue());}
                              if(cell.getColumnIndex() == 7){element.setPobAnimal_Total(cell.getNumericCellValue());}
                              if(cell.getColumnIndex() == 8){element.setPobAnimal_Enfermos(cell.getNumericCellValue());}
                              if(cell.getColumnIndex() == 9){element.setPobAnimal_Muertos(cell.getNumericCellValue());}
                              if(cell.getColumnIndex() == 11){element.setTotal_Muestras(cell.getNumericCellValue());}
                              if(cell.getColumnIndex() == 12){element.setRes_Positivos(cell.getNumericCellValue());}
                              if(cell.getColumnIndex() == 13)element.setRes_Negativos(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 14)element.setRes_Nt(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 15)element.setRes_Sospechoso(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 16)element.setCaso_Obs(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 17)element.setCaso_TipoCepa(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 20)element.setCaso_IPIC(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 23)element.setCaso_Estatus(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 24)element.setCaso_Id(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 25)element.setCuadrante_Id(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 26)element.setLocalidad_Id(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 27)element.setCaso_X(cell.getNumericCellValue());
                              if(cell.getColumnIndex() == 28)element.setCaso_Y(cell.getNumericCellValue());
                      }
                          break;
                      }
  
                  }
  
                  System.out.println(sheet.getLastRowNum());
  </code></pre>
  
  <p>To sum this...The first version of this code, has my SiveCuatro class, with all variables as String, and just one case at the Cell.CELL_TYPE_STRING, but doesn't insert the values that in the excel are like numbers, the excel file will always have the cells as "GENERAL" not text, not numeric, as GENERAL always.
  And in this code version I changed my constructor in my SiveCuatro class to match the fields numeric and it does insert but as float or doubles and i want them as String, also it doesn't stops at the last row with information.</p>
  
  <p>PS. Sorry for my bad english. please guys little help here.</p>