简体   繁体   中英

Autofilter excel using java poi

I am trying to filter an excel sheet using POI class and to set the filter when i used this :

CTAutoFilter sheetFilter = my_sheet.getCTWorksheet().getAutoFilter();

CTFilterColumn myFilterColumn = sheetFilter.insertNewFilterColumn(0);

got below mentioned error on "CTFilterColumn". Multiple markers at this line - The method insertNewFilterColumn(int) from the type CTAutoFilter refers to the missing type CTFilterColumn - The type org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFilterColumn cannot be resolved. It is indirectly referenced from required .class files - CTFilterColumn cannot be resolved to a type

Entire code :

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.ss.usermodel.ComparisonOperator;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAutoFilter;


public class Test1 {

    public static void main(String[] args) {
        try {
            //To read values and enable auto filter
            FileInputStream fileIn = new FileInputStream("./XMLs/Test001.xlsx");

            XSSFWorkbook my_workbook = new XSSFWorkbook(fileIn);

            XSSFSheet my_sheet = my_workbook.getSheet("Sheet1");
            CTAutoFilter sheetFilter = my_sheet.getCTWorksheet().getAutoFilter();

            CTFilterColumn myFilterColumn = sheetFilter.insertNewFilterColumn(0);

Instead of this "import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAutoFilter;" i tried import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;

But CTFilterColumn is not even listed in suggestions. Please help.

Is it because of some missing jar files, please help.

Promoting a comment to an answer

This is covered in this Apache POI FAQ Entry - I'm using the poi-ooxml-schemas jar, but my code is failing with "java.lang.NoClassDefFoundError: org/openxmlformats/schemas/ something " . The key section is:

There are two jar files available, as described in the components overview section. The full jar of all of the schemas is ooxml-schemas-1.3.jar, and it is currently around 15mb. The smaller poi-ooxml-schemas jar is only about 4mb. This latter jar file only contains the typically used parts though.

Many users choose to use the smaller poi-ooxml-schemas jar to save space. However, the poi-ooxml-schemas jar only contains the XSDs and classes that are typically used, as identified by the unit tests. Every so often, you may try to use part of the file format which isn't included in the minimal poi-ooxml-schemas jar. In this case, you should switch to the full ooxml-schemas-1.3.jar. Longer term, you may also wish to submit a new unit test which uses the extra parts of the XSDs, so that a future poi-ooxml-schemas jar will include them.

So, short term you need to swap out your small poi-ooxml-schemas jar for the full ooxml-schemas-1.3 jar. Longer term, if you submit a unit test to Apache POI which uses these extra classes, it'll be included in a future small schema jar.

Maven artifact details for the full schema are covered here on the Apache POI site

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM