使用Apache POI删除HSSF工作表的数据验证

use*_*294 5 java apache excel

如何通过使用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   
Run Code Online (Sandbox Code Playgroud)

小智 0

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