0

I have added dropdowns in some columns in excel sheet using Apache poi. When one of the drop down values contains character hyphen (-), then on opening the excel sheet, it gives error - We found a problem with some content in "Text.xlsx".

Without hyphen, all works fine. My code of creating dropdown is same as the accepted solution mentioned for this question -Limitation while generating excel drop down list with Apache POI

Please suggest some solution.

nshweta
  • 499
  • 2
  • 7
  • 19

1 Answers1

1

Below code is working fine for me:

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

Output:

enter image description here

Abhishek
  • 688
  • 6
  • 21
  • @nshweta please accept the answer if it worked for you. – Abhishek May 29 '18 at 10:16
  • What about commas? For me it is splitting into next line. https://stackoverflow.com/questions/63396530/apache-poi-insert-comma-in-excel-sheet – reiley Aug 13 '20 at 14:21