0

please advise is there any way lets say if I am getting a row Object as s parameter that is i know the row number now within that row i want to track the count of number of cells that are filed for that particular row.

lets say if the row number is 67 now in that row it can happen that only 2 cells are filled or it can happen that 4 cells are filled with value at last what i want is the count of cells that are filled as i have to implement some logic based on the count

below is the method where i am getting the row object and inside this method i need to get the count of cells

public  boolean isCountOfCellsFilledForARow(Row row1) {
        for (int c = row1.getFirstCellNum(); c < row1.getLastCellNum(); c++) 
{

}

}
sdsd sds
  • 1
  • 7

2 Answers2

2

As far as I understand you would like to find non-empty cells of the row. There is already an answered question regarding finding empty cells via apache-poi. You can use one of the provided answers to check if the cell is empty or not.

Community
  • 1
  • 1
feanor07
  • 3,328
  • 15
  • 27
0

You could try this

public long countCells(Row r) {
  long count = 0;
  for(Cell c : r) {
    if (!c.toString().equals("")) {
      count++;
    }
  }
  return count;
}
jmarkmurphy
  • 11,030
  • 31
  • 59
  • Well this way we can count the number of cells which is not suitable as poi has already provided us the methods to do that I mean built-in methods, the issue is to count the number of filled cells – sdsd sds May 13 '16 at 16:23
  • It will depend on what you mean by empty, but I added some logic to check for blank. A zero value in a Numeric cell is not empty, but a zero length string in a String cell is. In a formula cell, this will be based on the formula rather than the computed value of the formula. I am sure you can modify this to meet your definition of empty. – jmarkmurphy May 13 '16 at 17:35
  • Thanks but can you please advise in a row when I am iterating over cells and of these cells they can be of different data type that is they can be string cells, numeric cells, date cells so in that case how I will determine that If cell is empty – sdsd sds May 13 '16 at 17:39
  • The thing for which I am trying is that I have the row and now I am iterating over cells, these cells can be of different data type but that does not matter as I have to only check whether that cell is filled or empty and if in that row the total count of filled cells is 2 the no have to do some logic further – sdsd sds May 13 '16 at 17:43
  • `Cell.toString()` converts the cell it to a string. It deals with each cell type a bit differently. You can retrieve the cell type with `Cell.getCellType()` and do your own checking if you want. There are only a small number of Cell Types: `CELL_TYPE_BLANK, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR, CELL_TYPE_FORMULA, CELL_TYPE_NUMERIC, CELL_TYPE_STRING` – jmarkmurphy May 13 '16 at 17:43
  • I would use `Cell.toString`, and if it returns `""`, then the cell is blank. The only deviation for that would be if you need to check the value in a formula, then you will have to roll your own. – jmarkmurphy May 13 '16 at 17:45
  • Thanks I have only two types of cell that is string and numeric so please advise how can i customized the logic for these two only – sdsd sds May 13 '16 at 17:46
  • Do you consider a numeric 0 an empty value? – jmarkmurphy May 13 '16 at 17:47
  • Thanks well that case would not be there as luckily I do not have formula cells, so the above logic will work for both string and numeric cells – sdsd sds May 13 '16 at 17:47
  • Well no empty cells for me are the cells in the row that does not have a value they are completely empty rest there can be cells which have string value like 'ggg' and there can be numeric cells which can have value like '444' within the row – sdsd sds May 13 '16 at 17:49
  • Thanks a lot one more thing If we try to change a bit your logic that is getting the cell type by poi itself and it's value must be 0 or 1 depending upon it is filled with string and numeric then counting then how can we modify for that – sdsd sds May 13 '16 at 17:54
  • If you care about the cell type, then use `Cell.getCellType()` to retrieve that. – jmarkmurphy May 13 '16 at 18:04
  • Thanks lot then have to change the if condition that if cell type is 1 or 0 and then if cell is completely empty – sdsd sds May 13 '16 at 18:09
  • A `CELL_TYPE_STRING` can contain a value of "", which I would consider empty. There is also `CELL_TYPE_BLANK`. – jmarkmurphy May 13 '16 at 18:11
  • Also please if I wish to have these checks in your above piece of code then what changes I need to do to include blank cell check and numeric cell check too, thanks in advance – sdsd sds May 13 '16 at 18:17
  • use a switch statement to differentiate between the cell types – jmarkmurphy May 13 '16 at 18:25
  • Thanks agree but at last I want to count the number of filled cells as discuss earlier – sdsd sds May 13 '16 at 18:30
  • Thanks so well in that case how I would count the number of filled cells request you to show please a bit to grasp thanks in advance – sdsd sds May 13 '16 at 18:32
  • Use the logic above, it will count the filled cells. You don't need to do anything else. – jmarkmurphy May 13 '16 at 18:34