POI Auto Filter

前端 未结 6 683
逝去的感伤
逝去的感伤 2021-02-13 16:36

How do I use Apache POI to pre-define an auto-filter region in an Excel 2007 document?

A small code example or link would be nice.

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-13 17:28

    I found it difficult to get the CellRange since my columns and row values were both variable. So I used this trick.

    Initialize a lastCellReference variable to A1 which is row1, column1 cell

    String lastCellReference = "A1";
    

    Everytime you create a cell update the lastCellReference to maintain latest value

    cell = row.createCell((short) columnCount);
    cell.setCellValue(rs.getInt(i)); 
    lastCellReference=cell.getReference();
    

    A1 will always be first cell and then the lastCellReference

     sheet1.setAutoFilter(CellRangeAddress.valueOf("A1:"+ lastCellReference));
    

提交回复
热议问题