Apache POI Autofilter - 过滤后,excel被破坏,无法修复它而无法打开它

时间:2017-03-31 11:38:08

标签: java excel apache-poi autofilter

自动过滤excel表后,我无法打开它;它问道:

  

" Excel发现不可读的内容,您要恢复文件吗?"。

我尝试了一个没有任何配方细胞的小excel表,工作正常;即使过滤器已经过检查,没有数据可见,要查看数据,我不得不手动点击:

  

过滤器 - >全选 - >我的过滤器。

在下面发布我的代码:

public class Test1 {

    public static void main(String[] args) {

            try {
                //To read values and enable auto filter
                FileInputStream fileIn = new FileInputStream("./XMLs/testoo1.xlsx");
                //FileInputStream fileIn = new FileInputStream("./XMLs/NCR_ENG_Data_2017_01_FW04.xlsx");
                XSSFWorkbook my_workbook = new XSSFWorkbook(fileIn);
                //      XSSFSheet my_sheet = my_workbook.getSheet("NCR ENG Data ");
                XSSFSheet my_sheet = my_workbook.getSheet("Sheet1");
                my_sheet.setAutoFilter(CellRangeAddress.valueOf("A1:AC1"));
                CTAutoFilter sheetFilter = my_sheet.getCTWorksheet().getAutoFilter();


                CTFilterColumn myFilterColumn = sheetFilter.insertNewFilterColumn(0);
                myFilterColumn.setColId(1L);

                CTFilterColumn myFilterColumn1 = sheetFilter.insertNewFilterColumn(1);
                myFilterColumn1.setColId(5L);

                CTFilterColumn myFilterColumn2 = sheetFilter.insertNewFilterColumn(2);
                myFilterColumn1.setColId(19L);


                CTFilters firstColumnFilter = myFilterColumn.addNewFilters();
                CTFilter myFilter1 = firstColumnFilter.addNewFilter();

                CTFilters secondColumnFilter = myFilterColumn1.addNewFilters();
                CTFilter myFilter2 = secondColumnFilter.addNewFilter();

                CTFilters thirdColumnFilter = myFilterColumn2.addNewFilters();
                CTFilter myFilter3 = thirdColumnFilter.addNewFilter();

//              CTFilter myFilter=myFilterColumn.addNewFilters().insertNewFilter(0);

                myFilter1.setVal("1");
                myFilter2.setVal("1");
                myFilter3.setVal("CT");

                List<String> list1 = new ArrayList<String>();
                list1.add("1");

                List<String> list2 = new ArrayList<String>();
                list2.add("1");

                List<String> list3 = new ArrayList<String>();
                list2.add("CT");


                XSSFRow r1;
                /* Step-6: Loop through Rows and Apply Filter */
                for(Row r : my_sheet) {
                    for (Cell c : r) {


//                      FormulaEvaluator evaluator = my_workbook.getCreationHelper().createFormulaEvaluator();
//
//                      CellValue cellValue = evaluator.evaluate(c);
//                      
//                      
//                  
//                      System.out.println("-------------------------------- ");
//                      System.out.println(cellValue);
//                      
//                  
//                  int my_cellType = cellValue.getCellType();
//                  System.out.println(my_cellType);
//                  System.out.println(c.getCellType());

                    switch(c.getCellType()){
                    case 0:
                        System.out.println("-------------------------------- ");
                        System.out.println("Numeric cell ");
                        System.out.println(c.getNumericCellValue() + "");
                         if ( (c.getColumnIndex()==1 && !list1.contains(c.getNumericCellValue())) || (c.getColumnIndex()==5 && !list2.contains(c.getNumericCellValue()) || c.getColumnIndex()==19 && !list3.contains(c.getNumericCellValue())) ) {
                                r1=(XSSFRow) c.getRow();
                                if (r1.getRowNum()!=0) { /* Ignore top row */
                                /* Hide Row that does not meet Filter Criteria */
                                        r1.getCTRow().setHidden(true); }
                         }
                         break;
                    case 1:
                        System.out.println("-------------------------------- ");
                        System.out.println("String cell ");
                        System.out.println(c.getStringCellValue() + "");
                        if ( (c.getColumnIndex()==1 && !list1.contains(c.getStringCellValue())) || (c.getColumnIndex()==5 && !list2.contains(c.getStringCellValue()) || c.getColumnIndex()==19 && !list3.contains(c.getStringCellValue())) ) {
                            r1=(XSSFRow) c.getRow();
                            if (r1.getRowNum()!=0) { /* Ignore top row */
                            /* Hide Row that does not meet Filter Criteria */
                                    r1.getCTRow().setHidden(true); }
                     }
                         break;

                    case 2:
                        System.out.println("-------------------------------- ");
                        System.out.println("formula cell ");

                        switch (c.getCachedFormulaResultType()) {
                        case Cell.CELL_TYPE_STRING:
                            System.out.println(c.getRichStringCellValue().getString());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(c)) {
                                System.out.println(c.getDateCellValue() + "");
                            } else {
                                System.out.println(c.getNumericCellValue());
                            }
                           break;

                    }
                    break;

                    case 3:
                        System.out.println("-------------------------------- ");
                        System.out.println("blank cell ");
                        System.out.println(" ");
                         break;

                    case 4:
                        System.out.println("-------------------------------- ");
                        System.out.println("boolean cell ");
                        System.out.println(c.getBooleanCellValue() + "");
                        if ( (c.getColumnIndex()==1 && !list1.contains(c.getBooleanCellValue()))/* || (c.getColumnIndex()==5 && !list2.contains(c.getBooleanCellValue()) || c.getColumnIndex()==19 && !list3.contains(c.getBooleanCellValue()))*/ ) {
                            r1=(XSSFRow) c.getRow();
                            if (r1.getRowNum()!=0) { /* Ignore top row */
                            /* Hide Row that does not meet Filter Criteria */
                                    r1.getCTRow().setHidden(true); }
                     }
                         break;
                    case 5:
                        System.out.println("-------------------------------- ");
                        System.out.println("error cell ");
                        System.out.println(c.getErrorCellValue() + "");
                        if ( (c.getColumnIndex()==1 && !list1.contains(c.getErrorCellValue()))|| (c.getColumnIndex()==5 && !list2.contains(c.getErrorCellValue()) || c.getColumnIndex()==19 && !list3.contains(c.getErrorCellValue())) ) {
                            r1=(XSSFRow) c.getRow();
                            if (r1.getRowNum()!=0) { /* Ignore top row */
                            /* Hide Row that does not meet Filter Criteria */
                                    r1.getCTRow().setHidden(true); }
                     }
                         break;
                        }

                    }
                }














            FileOutputStream fileOut = new FileOutputStream("./XMLs/testoo1.xlsx");
            my_workbook.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

有什么不对吗?请检查。

0 个答案:

没有答案