为什么NPOI创建的单元格下拉列表始终按逗号分隔 [英] why NPOI created cell drop down list always split by comma

查看:74
本文介绍了为什么NPOI创建的单元格下拉列表始终按逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NPOI(Java的.net版本-Apache POI)创建了Excel工作表.我需要添加一些下拉列表,但是我发现无论传入哪个列表,它始终将项目值按逗号分隔,从而换行.您是否有机会避免这种情况的发生?这是我的代码

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

它总是将$ 1,900分成两部分,分别为$ 1和900,这是屏幕截图

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

推荐答案

将值传递给约束时,您只需传递没有逗号和$符号的值

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

这篇关于为什么NPOI创建的单元格下拉列表始终按逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆