First, I found this is not an Apache POI bug. It is a limitation from Excel. This is the link,
"There are limits to the number of items that will show in a data validation drop down list:
The list can show up to show 32,767 items from a list on the worksheet.
If you type the items into the data validation dialog box (a delimited list), the limit is 256 characters, including the separators."
Obviously, this line explicitly types more than 256 characters.
constraint =validationHelper.createExplicitListConstraint(countryName);
Second, this is my solution. It works fine.
public class Test {
public static void main(String[] args) throws IOException{
TreeSet<String> temp_rxGroups = new TreeSet<String>();
for (int i = 0; i < 302; i++) {
temp_rxGroups.add("" + i);
}
String[] countryName = temp_rxGroups.toArray(new String[temp_rxGroups.size()]);
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet realSheet = workbook.createSheet("realSheet");
XSSFSheet hidden = workbook.createSheet("hidden");
for (int i = 0, length= countryName.length; i < length; i++) {
String name = countryName[i];
XSSFRow row = hidden.createRow(i);
XSSFCell cell = row.createCell(0);
cell.setCellValue(name);
}
DataValidation dataValidation = null;
DataValidationConstraint constraint = null;
DataValidationHelper validationHelper = null;
validationHelper=new XSSFDataValidationHelper(realSheet);
CellRangeAddressList addressList = new CellRangeAddressList(0,0,0,0);
constraint =validationHelper.createFormulaListConstraint("hidden!$A$1:$A$" + countryName.length);
dataValidation = validationHelper.createValidation(constraint, addressList);
dataValidation.setSuppressDropDownArrow(true);
workbook.setSheetHidden(1, true);
realSheet.addValidationData(dataValidation);
FileOutputStream stream = new FileOutputStream("c:\test.xlsx");
workbook.write(stream);
stream.close();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…