如果通过使用JAVA和Apache Poi粘贴添加内容,数据validation不起作用

我正在使用Java代码创build一个Excel工作表。 我能够生成工作表,但现在我必须为特定的列添加数据validation。 这是我的代码:

HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Data Validation"); // set style to the cell CellStyle style = wb.createCellStyle(); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setBorderRight(CellStyle.BORDER_MEDIUM); style.setBorderBottom(CellStyle.BORDER_THIN); Row row = sheet.createRow((short) 0); // Create a cell and put a value in it. Cell cell = row.createCell(0); cell.setCellValue("First Name"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("Last Name"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("Age"); cell.setCellStyle(style); //CellRangeAddressList(index_of_starting_row, index_of_ending_row, index_of_starting_column,index_of_ending_column); CellRangeAddressList addressList = new CellRangeAddressList(1, 90, 2, 2); DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(new String[]{"10", "20", "30"}); DataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint); dataValidation.setSuppressDropDownArrow(true); sheet.addValidationData(dataValidation); // show error dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP); dataValidation.createErrorBox("Box Title", "Message Text"); String file = "test.xls"; FileOutputStream fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); 

当我input10,20,30以外的数字时,数据validation正在工作。 但是,如果我粘贴在列中的其他数字,那么validation超过。 什么可以解决这个问题?

我正在使用Apache poi库。

数据validation可以被任何为单元赋值的编程取代。 这不限于[tag:apache:poi]。 即使VBE的立即窗口可以绕过数据validation限制,

 activecell = 99 

… ActiveCell拥有如下所示的简单数据validation。

data_validation_bypass