0

I use NPOI(the .net version of java - Apache POI) created excel sheet. I need to add some dropdown, but I found no matter what list I passed into it, it always split the item value by comma, thus make a new line. In any chance, do you know how to avoid this happen? Here is my code

CellRangeAddressList cellRange = new CellRangeAddressList(cell.RowIndex, 
                        cell.RowIndex, cell.ColumnIndex, cell.ColumnIndex);
DVConstraint constraint = DVConstraint.CreateExplicitListConstraint(new string[]
                       {"$400","$1,900"});
HSSFDataValidation validation = new HSSFDataValidation(cellRange, constraint);
validation.SuppressDropDownArrow = false;
sheet.AddValidationData(validation);

It always break $1,900 into two items as $1 and 900, here is the screenshot enter image description here

jmarkmurphy
  • 11,030
  • 31
  • 59
Steven Zack
  • 4,984
  • 19
  • 57
  • 88

1 Answers1

0

While passing the values to constraint you just pass the values without comma and $ sign like this

DVConstraint constraint = DVConstraint.CreateExplicitListConstraint(new string[]{"400","1900"});

and below formatting approach will take care of appearance of value in dropdown

  XSSFCellStyle yourCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
    XSSFDataFormat yourDataFormat = (XSSFDataFormat)workbook.CreateDataFormat();
    yourCellStyle.SetDataFormat(yourDataFormat.GetFormat("$#,###.00"));
    sheet.SetDefaultColumnStyle(col, yourCellStyle);
kumar chandraketu
  • 2,232
  • 2
  • 20
  • 25