POI Auto Filter

calendar_today Asked Jun 24, 2010
thumb_up 14 upvotes
history Updated April 16, 2026

Question posted 2010 · +9 upvotes

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.

Accepted answer +14 upvotes

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)

2 code variants in this answer

  • Variant 1 — 1 lines, starts with sheet.setAutoFilter(new CellRangeAddress(firstCell.getRow()…
  • Variant 2 — 7 lines, starts with >x (x, y)

Top excel Q&A (6)

+14 upvotes ranks this answer #48 out of 167 excel solutions on this site .