2017-12-27 437 views
-3

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

Unset few values in the following filter
Data Sheet

+1

你必须发布一个代码。 –

+0

如果我有任何想法,我会做。虽然我可以读写excel文件。没有我可以发布的相关代码。 –

回答

1

到现在为止这只能使用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一起发货的较小的中。

+0

谢谢!虽然它工作正常,但它只在最新的poi版本中可用,我猜。 –

+0

@Parthenophobic Sallu:不需要。此代码需要[常见问题](https://poi.apache.org/faq.html#中提到的所有模式“ooxml-schemas-1.3.jar” FAQ-N10025)。 –

+0

谢谢。完美的作品。另外,我想知道为什么使用低级对象不被认为是一种好的做法? –

相关问题