0

I want filter excel data by program. for example

   |  a  b  c  |
   |  1  x  x  |
   |  2  x  x  |
   .............

I want to filter column a where a values 1 and ignore other , I have google a lot and search docs in apache poi website but no effect, is there any way to do this?

2 Answers 2

2

The index of rows and columns are 0-based. Therefore if you want to scan column A, you have to use the index 0.

Further, if you want to iterate cells in a specific column, you have to iterate the rows and then get the cell from each row for the specific column number.

To filter cells by a given value, the following steps are necessary:

  • check if the cell is not null
  • check if the cell has the correct cell type (CELL_TYPE_STRING / CELL_TYPE_NUMERIC)
  • check if the value contained by the cell is equivalent to the given filter value

Iterate column A

An example for your specific problem. The code iterates all cells in the first column (column A) and prints out the corresponding coordinates.

final int firstRowNumber = sheet.getFirstRowNum();
final int lastRowNumber = sheet.getLastRowNum();

// Column A (index = 0)
// final int columnNumber = CellReference.convertColStringToIndex("A")
final int columnNumber = 0;

final int filterValue = 1;

// Iterate rows
for (int rowNumber = firstRowNumber; rowNumber < lastRowNumber; rowNumber++) {
    final Row row = sheet.getRow(rowNumber);

    if (row != null) {
        final Cell cell = row.getCell(columnNumber);

        // Filter cells
        if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC && cell.getNumericCellValue() == filterValue) {
            System.out.println("Found cell with value " + filterValue + " at [" + rowNumber + "," + columnNumber + "]");
        }               
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

You can use CellReference.convertColStringToIndex(String) to turn the column label A into 0 to make it clearer
@Gagravarr thanks! By the way, is there also a function for the other way round? To generate the correct coordinate String from the row and column number?
Yup, just create a CellReference with two ints, then call formatAsString on it
I think the question was : Is there a way to filer on columns before parsing. Rows will parsed anyway. But in order to reduce number of iterations, is there a way to filter before iterating?
1

It's a very general question, so I can only give a general answer:

I think you should start with reading in the file and iterating over the Rows/Cells, then you can query the Cell-Value and go from there.

See the rest of the Guides at http://poi.apache.org/spreadsheet/quick-guide.html and http://poi.apache.org/spreadsheet/how-to.html for additional things that can be done.

1 Comment

tks,but Ive read there before, but its too general, I asked this question because need some example, at last I do it myself and it cost me the whole day, anyhow, tks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.