使用apache poi在工作表中自动过滤

时间:2018-08-17 09:54:53

标签: java apache-poi

我正在使用apache poi,其中我将autofilter设置为所有列。我也通过过滤1列来获取数据,但是当我要过滤2列并获取数据时,表中未应用任何过滤器。

outputSheet.setAutoFilter(CellRangeAddress.valueOf("A1:F136"));
CTAutoFilter sheetFilter=outputSheet.getCTWorksheet().getAutoFilter();


CTFilterColumn  myFilterColumn=sheetFilter.insertNewFilterColumn(0);
myFilterColumn.setColId(1);

CTFilterColumn  myFilterColumn2=sheetFilter.insertNewFilterColumn(1);
myFilterColumn2.setColId(6);

CTFilters firstColumnFilter=myFilterColumn.addNewFilters();
CTFilter myFilter1=firstColumnFilter.addNewFilter();

CTFilters secondColumnFilter=myFilterColumn2.addNewFilters();
CTFilter myFilter2=secondColumnFilter.addNewFilter();


myFilter1.setVal("fidelity.com");
myFilter2.setVal("US");

List<String> list1 = new ArrayList<String>();
list1.add("Ad Cloud");

List<String> list2 = new ArrayList<String>();
list2.add("US");

XSSFRow r1;

for(Row r : my_sheet) {
    for (Cell c : r) {

        c.setCellType(Cell.CELL_TYPE_STRING);
        if ( (c.getColumnIndex()==1 && !list2.contains(c.getStringCellValue())) || (c.getColumnIndex()==6 && !list1.contains(c.getStringCellValue())) ){
            r1=(XSSFRow) c.getRow();

            if (r1.getRowNum()!=0) { /* Ignore top row */
                /* Hide Row that does not meet Filter Criteria */
                r1.getCTRow().setHidden(true); 
            }
        }
    }
}

outputworkbook.write(fos);
    fos.close();
    //format(outputFile);
}

0 个答案:

没有答案