我希望按程序筛选excel数据。例如
| a b c |
| 1 x x |
| 2 x x |
.............
我想过滤一个值为1的列并忽略其他列, 我已经google了很多,并在apache poi网站上搜索文档但没有效果,有什么方法可以做到这一点吗?
答案 0 :(得分:1)
这是一个非常普遍的问题,所以我只能给出一般答案:
我认为你应该从reading in the file和iterating over the Rows/Cells开始,然后你可以query the Cell-Value从那里开始。
请参阅http://poi.apache.org/spreadsheet/quick-guide.html和http://poi.apache.org/spreadsheet/how-to.html处的其他指南,了解可以执行的其他操作。
答案 1 :(得分:1)
行和列的索引从0开始。因此,如果要扫描A列,则必须使用索引0。
此外,如果要迭代特定列中的单元格,则必须迭代行,然后从每行获取特定列编号的单元格。
要按给定值过滤单元格,需要执行以下步骤:
CELL_TYPE_STRING
/ CELL_TYPE_NUMERIC
)针对您的具体问题的示例。代码迭代第一列(A列)中的所有单元格并打印出相应的坐标。
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 + "]");
}
}
}