我可以在for
循环的帮助下设置过滤条件。以下代码遍历 excel 表的每一行和每一列并执行过滤操作。有什么方法可以直接启用特定条件而不会陷入循环?
/* Create Workbook and Worksheet */
FileInputStream in = new FileInputStream(new File("D:\\PNR_AUTOMATION\\match.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(in);
HSSFSheet sheet = workbook.getSheetAt(0);
/*Add filter on the top row*/
AutoFilter filter = sheet.setAutoFilter(CellRangeAddress.valueOf("A1:B1"));
/*Create list of items that has be filtered*/
List<String> list = new ArrayList<String>();
list.add("a");
HSSFRow r1;
/*Loop through Rows and Apply Filter */
for(Row r : sheet) {
for (Cell c : r) {
if (c.getColumnIndex()==0 && (!list.contains(c.getStringCellValue()))) {
r1=(HSSFRow) c.getRow();
if (r1.getRowNum()!=0) {
/*Ignore top row */
r1.setZeroHeight(true); }
}
}
}
FileOutputStream out = new FileOutputStream(
new File("D:\\PNR_AUTOMATION\\match.xls"));
workbook.write(out);
out.close();