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

问题描述 投票:-3回答:1

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

Unset few values in the following filter Data Sheet

java excel apache-poi
1个回答
1
投票

到目前为止,这只能通过使用apache poi的底层低级对象来实现。对AutoFilterthese来说是 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();
 }
}

这段代码需要ooxml-schemas-1.3.jar中提到的所有模式Frequently Asked Questions的完整jar。这是因为较低级别的org.openxmlformats.schemas.spreadsheetml.x2006.main.CT*Filter*类不包含在较小的poi-ooxml-schemas jar中,默认情况下随apache poi一起提供。

© www.soinside.com 2019 - 2024. All rights reserved.