问题
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
回答1:
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);
来源:https://stackoverflow.com/questions/43283268/why-npoi-created-cell-drop-down-list-always-split-by-comma