why NPOI created cell drop down list always split by comma

我的梦境 提交于 2019-12-12 04:21:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!