Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting filter on headers of an Excel sheet via POI

I generate a sheet, pretty bog standard headers and columns of data.

I want to turn on the "Filter" function for the sheet, so the user can easily sort and filter the data.

Can I so this using POI?

like image 821
Anders Johansen Avatar asked Dec 04 '12 12:12

Anders Johansen


People also ask

What are the 3 ways to add filter in Excel?

In Excel, you can create three kinds of filters: by values, by a format, or by criteria. But each of these filter types is mutually exclusive. For example, you can filter by cell color or by a list of numbers, but not by both. You can filter by icon or by a custom filter, but not by both.


2 Answers

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

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

For example, from the below sheet.

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

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

like image 140
Victor Avatar answered Sep 19 '22 00:09

Victor


easiest way of adding filter on header :

sheet.setAutoFilter(new CellRangeAddress(0, 0, 0, numColumns)); sheet.createFreezePane(0, 1); 
like image 38
Tiago Medici Avatar answered Sep 19 '22 00:09

Tiago Medici