【问题标题】:remove data validation of HSSF sheet using Apache POI使用 Apache POI 删除 HSSF 表的数据验证
【发布时间】:2015-01-15 11:32:15
【问题描述】:

如何从使用 apache POI 中删除 excel 的数据验证。 我有以下代码。在第一次运行时,下拉菜单会正确创建,但是一旦我再次使用字符串列表中的不同值运行程序,下拉菜单就不会更新。

FileInputStream fsIP= new FileInputStream(new File("D:\\template3.xls")); //Read the spreadsheet that needs to be updated              
                HSSFWorkbook wb = new HSSFWorkbook(fsIP); //Access the workbook           
                HSSFSheet worksheet = wb.getSheetAt(0); //Access the worksheet, so that we can update / modify it.
          //     System.out.println(worksheet.getRow(1).getCell(2));
                Cell cell = null; // declare a Cell object                
                DataValidation dataValidation = null;
                DataValidationConstraint constraint = null;
                DataValidationHelper validationHelper = null;
                CellRangeAddressList addressList = new  CellRangeAddressList(0,5,0,5);

                //cell = worksheet.getRow(2).getCell(2);   // Access the second cell in second row to update the value 
                DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(new String[]{"124", "20", "30"});
                  dataValidation = new HSSFDataValidation(addressList, dvConstraint);
                  dataValidation.setSuppressDropDownArrow(false);

                  worksheet.addValidationData(dataValidation);
               // cell.setCellValue("OverRide Last Name");  // Get current cell value value and overwrite the value

                fsIP.close(); //Close the InputStream

                FileOutputStream output_file =new FileOutputStream(new File("D:\\template3.xls"));  //Open FileOutputStream to write updates
                wb.write(output_file); //write changes

               output_file.close();  //close the stream   

【问题讨论】:

    标签: java excel apache


    【解决方案1】:

    我不得不在工作表中删除/编辑 DataValidation,但在任何地方都找不到答案,这就是我所做的。 (使用 apache-poi:3.14)

    首先,除了阅读问题,你不应该使用这种方法:

    worksheet.getSheet("MySheet").getDataValidations()
    

    它总是发送 DataValidation 的副本,因此编辑它们将无济于事。

    我不得不这样做:

    worksheet.getSheet("MySheet").getCTWorksheet().getDataValidations().getDataValidationList()
    

    然后您将能够使用 get/set/remove 方法从此处编辑或删除 DataValidation。

    问候。

    【讨论】:

    【解决方案2】:

    下拉列表的字符总数(包括分隔符)不应超过 256 个字符。这是 Excel 的限制。 如果您的下拉列表中有更多字符,您可以使用参考表来填充下拉列表中的数据。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多