3

In reference to my previous question How to calculate number of rows in a column of Excel document using Java i was able to calculate the total number of columns in the given sheet. Now half of the work is yet to be done as i want to calculate the number of rows in a particular column. Possible solution could be using 2d array and storing column index and the total rows or using map, etc. How i can achieve this? Java code is provided here. I'm getting right count(column count) for my demo file. Please modify/suggest changes as required.

(edit): i've used hasp map to calculate store column index as key and row count as value, but it wasnt working, may be the applied logic was wrong. Well, if i want to accomplish this by using Hash Map, how i can store number of rows in a particular column(while iterating) as a value

Java Code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelRead {
    static int colrange=1000;
    public static void main(String[] args) {
        HashMap hm=new HashMap();
        int count=0;
    try {
        FileInputStream file = new FileInputStream(new File("C:/Users/vinayakp/Desktop/Demo2.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> 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("");
        }

        for(Row r:sheet)
        {
            short minColIx=r.getFirstCellNum();
            short maxColIx=r.getLastCellNum();
            for(short colIx=minColIx;colIx<maxColIx;colIx++) {
                Cell c= r.getCell(colIx);
                if(c!=null) {
                    if(c.getCellType()== Cell.CELL_TYPE_STRING||c.getCellType() == Cell.CELL_TYPE_NUMERIC||c.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                        count++; ---// can i use hashcode in here to get the key and value pair? key=column index value=total number of rows in that column
                            } 
                    }
                    else break;
                }
            }

        System.out.println("\nTotal Number of columns are:\t"+count);
        System.out.println(hm);
        file.close();    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException ae) {
        ae.printStackTrace();
    }
}
}
Community
  • 1
  • 1
Vinayak Pahalwan
  • 2,915
  • 4
  • 26
  • 34

3 Answers3

7

Try this:

private void excelReader() {
    String data;
    try {
        InputStream is = new FileInputStream("Read.xlsx");
        Workbook wb = WorkbookFactory.create(is);
        Sheet sheet = wb.getSheetAt(0);
        Iterator rowIter = sheet.rowIterator();
        Row r = (Row)rowIter.next();
        short lastCellNum = r.getLastCellNum();
        int[] dataCount = new int[lastCellNum];
        int col = 0;
        rowIter = sheet.rowIterator();
        while(rowIter.hasNext()) {
            Iterator cellIter = ((Row)rowIter.next()).cellIterator();
            while(cellIter.hasNext()) {
                Cell cell = (Cell)cellIter.next();
                col = cell.getColumnIndex();
                dataCount[col] += 1;
                DataFormatter df = new DataFormatter();
                data = df.formatCellValue(cell);
                System.out.println("Data: " + data);
            }
        }
        is.close();
        for(int x = 0; x < dataCount.length; x++) {
            System.out.println("col " + x + ": " + dataCount[x]);
        }
    }
    catch(Exception e) {
        e.printStackTrace();
        return;
    }
}

Tested code

I created an xlsx file with the following cell data:

Col0    Col1    Col2    Col3    Col4
1       a       x       a       q
2       b       y       s       w
3       c       z       d       e
4       d               f       r
5       e                       t
                                y

The contents of dataCount array is this:

col 0: 6

col 1: 6

col 2: 4

col 3: 5

col 4: 7

The numbers on the right count the number of cells with data for each column, including the header row.

If you want to exclude the header row, just remove the line:

rowIter = sheet.rowIterator();

just before the while loop.

Is this what you are looking for?

  • i did this...for 1 column it is giving me the right ans...now for entire sheet i used `for loop` i didnt get the desired result. Can you edit the ans and let me know how i can iterate thru the list of sheet having 10 columns? – Vinayak Pahalwan Dec 14 '12 at 09:10
  • I have modified the code. Please check if its working because I have not tested this. =) – TheQuickBrownFox Dec 14 '12 at 09:41
  • the o/p is **Data: A Data: B Data: C....and so on** (printing the headers the cell contents) – Vinayak Pahalwan Dec 14 '12 at 10:00
  • the problem is it reads the data row-wise and what i've done is i'm able to extract 1 cell and calculated the number of data rows, when i'm iterating it it is giving me false results. So do i have to count data 1 cell at a time? – Vinayak Pahalwan Dec 14 '12 at 10:05
  • As far as I know, you have to do this per cell, per row. I'm not at a computer where I can code right now so I can't confirm. – TheQuickBrownFox Dec 14 '12 at 11:44
  • np...still looking for the ans, tried thru _haspmap_ and other methods, lets c – Vinayak Pahalwan Dec 14 '12 at 11:50
  • Hi. =) You say that the results you get are incorrect. I've tested the code and I get correct results. Maybe I'm mistaken as to what you are trying to achieve here. You're trying to count the number of cells with data under each column, correct? – TheQuickBrownFox Dec 15 '12 at 02:33
  • thanks a lot, it worked :) the above code will help others too :) thanks again – Vinayak Pahalwan Dec 17 '12 at 05:48
  • I have used your code ans tested it is reading all the data but the problem is that it is taking blank coloum while reading ..can you solve that??? – lucifer Aug 29 '14 at 06:39
1

Will this solve?

HSSFSheet mySheet = myWorkBook.getSheetAt(0);
                Iterator<Row> rowIter = mySheet.rowIterator();
                while (rowIter.hasNext()) {
                    HSSFRow myRow = (HSSFRow) rowIter.next();
                    Iterator<Cell> cellIter = myRow.cellIterator();
                    List<HSSFCell> cellStore = new ArrayList<HSSFCell>();
                    while (cellIter.hasNext()) {
                        HSSFCell myCell = (HSSFCell) cellIter.next();
                        rowCount++ //For myRow
                    }

                }
Binu
  • 120
  • 6
  • it is giving the total number of data in the excel sheet, i mean including the header column name as well. What i want is to calculate the count of data in a particular column like _ColumnA has 5 rows_ – Vinayak Pahalwan Dec 14 '12 at 07:33
0

it is giving the total number of data in the excel sheet, i mean including the header column name as well.

In my understanding, you want to count the number of rows that a column has, EXCEPT for the header row.

If that's the case, you can use the solution provided by Binu. Just modify it to skip the row where the column name is.