如何使用apache POI获得可见的行数?

时间:2018-09-18 14:47:29

标签: java excel apache-poi autofilter rowcount

我已经使用自动过滤器在excel中过滤该数据,并且我想要除隐藏行之外的可见行数。我尝试搜索此方法,但未找到任何特定方法。我什至尝试使用excel公式并获取行数,但这仅提供了缓存的值。

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


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

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

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

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


myFilter1.setVal("New Account");
myFilter2.setVal("Hedge Fund");

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()==2 && !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 个答案:

没有答案