POI Auto Filter

David picture David · Jun 25, 2010 · Viewed 24k times · Source

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.

Answer

Victor picture Victor · Dec 4, 2012

Save the first and the last cell from the filter area, and execute:

sheet.setAutoFilter(new CellRangeAddress(firstCell.getRow(), lastCell.getRow(), firstCell.getCol(), lastCell.getCol()));

For example, from the sheet below.

>x         (x, y)
  0123456  
0|--hhh--|   h = header
1|--+++--|   + = values
2|--+++--|   - = empty fields
3|--+++--|
4|-------|

fist cell will be the header above the first + (2,1) cell. The the last will be the last + cell (5,3)