title: Reading cell values from excel file using Apache POI
body: <p>I'm using Selenium 2 and have set up a LoginTest.java file that reads from a data driven excel file. I read in username and password and run it on a login page. It actually runs fine. The data looks like the following:</p>
  
  <p>user1 pw1<br>
  user2 pw2<br>
  user3 pw3<br>
  user4 pw4</p>
  
  <p>The problem is this: if I modify the excel file and change the number of arguments to something like this:</p>
  
  <p>user1 pw1<br>
  user2 pw2  </p>
  
  <p>and run the same script, the <code>ReadExcel</code> class file returns an error msg: </p>
  
  <blockquote>
    <p>"There is no support for this type of cell".</p>
  </blockquote>
  
  <p>What is happening I think, is that rows 3 and 4 <em>used</em> to contain data in their cells (user3 pw3 and user4 pw4) and now they don't...so there is something different about those previously used cells that my <code>ExcelRead()</code> class isn't catching to ignore. I'll bet the cell is now 'null' where it previously wasn't or vice versa. Something is different about the cells which previously held data vs cells that have never held data.</p>
  
  <p>(I found <code>ExcelRead</code> on the internet and am using it. I didn't create it from scratch myself. It seems to work fine reading xcel files except for this one 'issue').</p>
  
  <p>Thanks for any help.</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ExcelRead {
      public Object[][] main( String[] args) throws Exception{        
      File excel = new File(args[0]);
      FileInputStream fis = new FileInputStream(excel);
      HSSFWorkbook wb = new HSSFWorkbook(fis);
      HSSFSheet ws = wb.getSheet(args[1]);
  
      int rowNum = ws.getLastRowNum() + 1;
      int colNum = ws.getRow(0).getLastCellNum();
      String[][] data = new String[rowNum][colNum];
  
      for (int i = 0 ; i &lt; rowNum ; i++) {
          HSSFRow row = ws.getRow(i);
              for (int j = 0 ; j &lt; colNum ; j++) {
                  HSSFCell cell = row.getCell(j);
                  String value = cellToString(cell);
                  data[i][j] = value ;
                  //System.out.println("the value is " + value);
              }
          }
      return data;
      }
  
  public static String cellToString(HSSFCell cell) {  
      int type;
      Object result;
      type = cell.getCellType();
  
      switch (type) {
  
          case 0: // numeric value in Excel
              result = cell.getNumericCellValue();
              break;
          case 1: // String Value in Excel 
              result = cell.getStringCellValue();
              break;
          default:  
              throw new RuntimeException("There is no support for this type of cell");                        
      }
  
      return result.toString();
  }
  </code></pre>