【发布时间】: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
【问题讨论】: