如何在JAVA中使用apache POI在Excel中设置/取消设置列过滤器的值?

我有一个Excel表格,其中所有列都添加了过滤器。我想使用apache POI JAVA取消/设置一些过滤器的值。我尝试了很多东西,但徒劳无功。任何帮助将不胜感激。如何在JAVA中使用apache POI在Excel中设置/取消设置列过滤器的值?

Unset few values in the following filter
Data Sheet

回答:

到现在为止这只能使用apache poi下衬低级别的对象来实现。对于AutoFilter这些是 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAutoFilter和后继者。

实施例:

import org.apache.poi.ss.usermodel.*; 

import org.apache.poi.ss.util.*;

import org.apache.poi.xssf.usermodel.*;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAutoFilter;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFilterColumn;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFilters;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomFilters;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomFilter;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFilterOperator;

import java.io.FileOutputStream;

class AutoFilterSetTest {

private static void setCellData(Sheet sheet) {

Row row = sheet.createRow(0);

Cell cell = row.createCell(0);

cell.setCellValue("Number");

cell = row.createCell(1);

cell.setCellValue("Alphabets");

for (int r = 1; r < 11; r++) {

row = sheet.createRow(r);

cell = row.createCell(0);

cell.setCellValue(r);

cell = row.createCell(1);

cell.setCellValue(new String(Character.toChars(64 + r)));

}

}

private static void setCriteriaFilter(XSSFSheet sheet, int colId, int firstRow, int lastRow, String[] criteria) throws Exception {

CTAutoFilter ctAutoFilter = sheet.getCTWorksheet().getAutoFilter();

CTFilterColumn ctFilterColumn = ctAutoFilter.addNewFilterColumn();

ctFilterColumn.setColId(colId);

CTFilters ctFilters = ctFilterColumn.addNewFilters();

for (int i = 0; i < criteria.length; i++) {

ctFilters.addNewFilter().setVal(criteria[i]);

}

//hiding the rows not matching the criterias

DataFormatter dataformatter = new DataFormatter();

for (int r = firstRow; r <= lastRow; r++) {

XSSFRow row = sheet.getRow(r);

boolean hidden = true;

for (int i = 0; i < criteria.length; i++) {

String cellValue = dataformatter.formatCellValue(row.getCell(colId));

if (criteria[i].equals(cellValue)) hidden = false;

}

if (hidden) row.getCTRow().setHidden(hidden);

}

}

public static void main(String[] args) throws Exception {

XSSFWorkbook wb = new XSSFWorkbook();

XSSFSheet sheet = wb.createSheet();

//create rows of data

setCellData(sheet);

for (int c = 0; c < 2; c++) sheet.autoSizeColumn(c);

int lastRow = sheet.getLastRowNum();

XSSFAutoFilter autofilter = sheet.setAutoFilter(new CellRangeAddress(0, lastRow, 0, 1));

//XSSFAutoFilter is useless until now

setCriteriaFilter(sheet, 0, 1, lastRow, new String[]{"2", "4", "7"});

wb.write(new FileOutputStream("AutoFilterSetTest.xlsx"));

wb.close();

}

}

此代码需要如Frequently Asked Questions提到的所有的模式ooxml-schemas-1.3.jar的充分广口瓶中。这是因为低级别org.openxmlformats.schemas.spreadsheetml.x2006.main.CT*Filter*类别不包含在默认情况下随apache poi一起发货的较小的中。

以上是 如何在JAVA中使用apache POI在Excel中设置/取消设置列过滤器的值? 的全部内容, 来源链接: utcz.com/qa/267037.html

回到顶部