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)
- Shortcut to Apply a Formula to an Entire Column in Excel +335 (2011)
- How should I escape commas and speech marks in CSV files so they work in Excel? +136 (2012)
- Convert xlsx to csv in linux command line +96 (2012)
- How to create a link inside a cell using EPPlus +50 (2011)
- IF statement: how to leave cell blank if condition is false ("" does not work) +44 (2013)
- T-SQL: Export to new Excel file +44 (2012)
excel solutions on this site
.