我使用Apache poi在excel表的某些列中添加了下拉列表。当其中一个下拉值包含字符连字符( - )时,则打开excel表时会出错 - 我们发现" Text.xlsx"中的某些内容有问题。
没有连字符,一切正常。 我创建下拉列表的代码与此问题中提到的已接受解决方案相同 - Limitation while generating excel drop down list with Apache POI
请提出一些解决方案。
答案 0 :(得分:1)
以下代码对我来说很好:
DataValidation dataValidation = null;
DataValidationConstraint constraint = null;
DataValidationHelper validationHelper = null;
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet1=(XSSFSheet) wb.createSheet("sheet1");
validationHelper=new XSSFDataValidationHelper(sheet1);
CellRangeAddressList addressList = new CellRangeAddressList(0,5,0,0);
constraint =validationHelper.createExplicitListConstraint(new String[]{"SELECT","10-11", "20", "30"});
dataValidation = validationHelper.createValidation(constraint, addressList);
dataValidation.setSuppressDropDownArrow(true);
sheet1.addValidationData(dataValidation);
FileOutputStream fileOut = new FileOutputStream("c:\\temp\\abhishek.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
输出: