此方法对03、07版的excel都支持
public static void setValidationData(Sheet sheet, int firstRow, int lastRow,
int firstCol, int lastCol,String[] explicitListValues) throws IllegalArgumentException{
if (firstRow < 0 || lastRow < 0 || firstCol < 0 || lastCol < 0 || lastRow < firstRow || lastCol < firstCol) {
throw new IllegalArgumentException("Wrong Row or Column index : " + firstRow+":"+lastRow+":"+firstCol+":" +lastCol);
}
//07版excel
if (sheet instanceof XSSFSheet) {
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet)sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
.createExplicitListConstraint(explicitListValues);
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
sheet.addValidationData(validation);
//03版ecxel
} else if(sheet instanceof HSSFSheet){
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(explicitListValues);
DataValidation validation = new HSSFDataValidation(addressList, dvConstraint);
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
sheet.addValidationData(validation);
}
}