2017-12-27 435 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

謝謝。完美的作品。另外,我想知道爲什麼使用低級對象不被認爲是一種好的做法? –

相關問題