通过POI设置Excel工作表标题的过滤器
我生成了一个工作表,非常标准的标题和数据列 我想打开表单的“过滤”功能,这样用户就可以轻松地对数据进行排序和过滤通过POI设置Excel工作表标题的过滤器,excel,apache-poi,autofilter,Excel,Apache Poi,Autofilter,我生成了一个工作表,非常标准的标题和数据列 我想打开表单的“过滤”功能,这样用户就可以轻松地对数据进行排序和过滤 我是否可以使用POI来实现此目的?保存筛选区域的第一个和最后一个单元格,并执行: sheet.setAutoFilter(new CellRangeAddress(firstCell.getRow(), lastCell.getRow(), firstCell.getCol(), lastCell.getCol())); 例如,从下表中 >x (x, y)
我是否可以使用POI来实现此目的?保存筛选区域的第一个和最后一个单元格,并执行:
sheet.setAutoFilter(new CellRangeAddress(firstCell.getRow(), lastCell.getRow(), firstCell.getCol(), lastCell.getCol()));
例如,从下表中
>x (x, y)
0123456
0|--hhh--| h = header
1|--+++--| + = values
2|--+++--| - = empty fields
3|--+++--|
4|-------|
第一个单元格将是第一个
+
(2,1)单元格上方的标题。最后一个将是最后一个+
单元格(5,3)如果您还想以编程方式设置过滤器,可以使用以下方法:
void setAutoFilter(final XSSFSheet sheet, final int column, final String value) {
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:Z1"));
final CTAutoFilter sheetFilter = sheet.getCTWorksheet().getAutoFilter();
final CTFilterColumn filterColumn = sheetFilter.addNewFilterColumn();
filterColumn.setColId(column);
final CTFilter filter = filterColumn.addNewFilters().insertNewFilter(0);
filter.setVal(value);
// We have to apply the filter ourselves by hiding the rows:
for (final Row row : sheet) {
for (final Cell c : row) {
if (c.getColumnIndex() == column && !c.getStringCellValue().equals(value)) {
final XSSFRow r1 = (XSSFRow) c.getRow();
if (r1.getRowNum() != 0) { // skip header
r1.getCTRow().setHidden(true);
}
}
}
}
}
相关梯度相关性:
// https://mvnrepository.com/artifact/org.apache.poi/poi
compile group: 'org.apache.poi', name: 'poi', version: '3.9'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.9'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas
compile group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '3.9'
// https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas
compile group: 'org.apache.poi', name: 'ooxml-schemas', version: '1.3'
我想出了如何使用NPOI实现这一点。
将CT\U自动过滤器添加到CT\U表中。
我猜它对POI和NPOI的作用是一样的
cttable.autoFilter = new CT_AutoFilter();
cttable.autoFilter.@ref = "A1:C5"; // value is data and includes header.
在标题上添加筛选器的最简单方法:
sheet.setAutoFilter(new CellRangeAddress(0, 0, 0, numColumns));
sheet.createFreezePane(0, 1);
使用
sheet.setAutoFilter(CellRangeAddress.valueOf(“B1:H1”)代码>
我们必须只指定表格数据的标题单元格。在我的示例中,标题从单元格B1开始,在单元格H1结束
Excel将自动找到其下方的数据,并将其显示在筛选选项中。Nice。不过,似乎我可以通过以下方式逃脱:s.setAutoFilter(CellRangeAddress.valueOf(“A1:E1”);此范围仅为标题单元格,不包括数据单元格。尽管如此,结果仍然是我想要的;这样我们也可以过滤数据。你的意思是最后一个+单元格是(4,3)吗?我想单元格的API可能已经改变了。我必须使用这个范围:newcellRangeAddress(firstCell.getRowIndex(),lastCell.getRowIndex(),firstCell.getColumnIndex(),lastCell.getColumnIndex())
。getRow现在返回一行而不是索引,并且getCol不再存在。@Victor by(5,3)是指(3,4)吗?可能重复的小更正:这将是numColumns-1
(否则在末尾会有一个额外的筛选列)。既然numColumns是一个变量,为什么要减去一个值?既然你可以定义它,举例来说;如果你期望有9,你不需要把它设为10,然后减去1……这更像是一个概念性的注释,而不是逐字的代码:)。要点是CellRangeAddress
的构造函数是零索引的,并且包含绑定。因此,如果你是基于某种东西来计算numColumns
,你需要考虑到这一点。我同意@priiduemere的观点。我认为lastIndex
比numColumns
更合适(或者我会使用numColumns-1
)