首页 文章

下拉列表值包含带有Apache POI的连字符( - )时出现Excel错误

提问于
浏览
0

我使用Apache poi在excel表的某些列中添加了下拉列表 . 当其中一个下拉值包含字符连字符( - ),然后打开excel表时,它会出错 - 我们发现“Text.xlsx”中的某些内容有问题 .

没有连字符,一切正常 . 我创建下拉列表的代码与此问题中提到的已接受解决方案相同 - Limitation while generating excel drop down list with Apache POI

请提出一些解决方案 .

1 回答

  • 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();
    

    输出:

    enter image description here

相关问题