title: How to calculate number of rows in a column of Excel document using Java
body: <p>I have a java code which fetches data from excel document. <strong>I want to calculate the number of columns and total number of rows(in a particular column)</strong>. How can I achieve this? Java code and desired o/p is provided below</p>
  
  <p>(edit): what modification I should make to get the desired o/p for e.g. I should write a loop to get the count of columns and rows or there is a method to do the same</p>
  
  <p>Desired O/P</p>
  
  <pre><code>ColumnA ColumnB ColumnC
  Vinayak James   Dan
  India   US      Denmark
  
   Total number of Columns: 3
  number of data in ColumnA:2
  number of data in ColumnB:2
  number of data in ColumnC:2  
  </code></pre>
  
  <p>(EDIT):- Answered here-- <a href="https://stackoverflow.com/questions/13874074/count-number-of-rows-in-a-column-of-excel-sheetjava-code-provided">Count number of rows in a column of Excel sheet(Java code provided)</a></p>
  
  <p>My Java Code:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.util.Iterator;
  
  import org.apache.poi.ss.formula.functions.Column;
  import org.apache.poi.ss.usermodel.Cell;
  import org.apache.poi.ss.usermodel.Row;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
  public class ExcelRead {
      public static void main(String[] args) {
          int count=0;
      try {
          FileInputStream file = new FileInputStream(new File("C:/Users/vinayakp/Desktop/Book.xlsx"));
          XSSFWorkbook workbook = new XSSFWorkbook(file);
          XSSFSheet sheet = workbook.getSheetAt(0);
          Iterator&lt;Row&gt; rowIterator = sheet.iterator();
          while(rowIterator.hasNext()) {
              Row row = rowIterator.next();
              Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
              while(cellIterator.hasNext()) {
                  Cell cell = cellIterator.next();
                  switch(cell.getCellType()) {
                      case Cell.CELL_TYPE_BOOLEAN:
                          System.out.print(cell.getBooleanCellValue() + "\t\t");
                          break;
                      case Cell.CELL_TYPE_NUMERIC:
                          System.out.print(cell.getNumericCellValue() + "\t\t");
                          break;
                      case Cell.CELL_TYPE_STRING:
                          System.out.print(cell.getStringCellValue() + "\t\t");
                          break;
                  }
              }
              System.out.println("");
          }
  
          file.close();    
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException ae) {
          ae.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>Output I'm getting is:</p>
  
  <pre><code>ColumnA ColumnB ColumnC
  Vinayak James   Dan
  India   US      Denmark
  </code></pre>
  
  <p>I need  to get the desired o/p as shown above. <strong>Code is working fine</strong> however I need to get the <em>count</em> values of column and rows. Kindly provide me the solution for the same. I had problems with the code earlier which was resolved in this question: <a href="https://stackoverflow.com/questions/13856466/issu-while-reading-excel-document-java-code">Issue while reading Excel document (Java code)</a></p>