这篇文章给大家分享的是有关C#如何使用NPOI设置Excel下拉选项的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

第一种

· 直接设置下拉值,不超过255个字符(优点:逻辑简单 ;缺点:有字符限制)

· 适用于下拉值为固定值,例如:状态、性别等

方法块:

publicstaticvoidSetCellDropdownList(ISheetsheet,intfirstcol,intlastcol,string[]vals){//设置生成下拉框的行和列varcellRegions=newCellRangeAddressList(1,65535,firstcol,lastcol);//设置下拉框内容DVConstraintconstraint=DVConstraint.CreateExplicitListConstraint(vals);//绑定下拉框和作用区域,并设置错误提示信息HSSFDataValidationdataValidate=newHSSFDataValidation(cellRegions,constraint);dataValidate.CreateErrorBox("输入不合法","请输入或选择下拉列表中的值。");dataValidate.ShowPromptBox=true;sheet.AddValidationData(dataValidate);}

调用:

HSSFWorkbookworkbook=newHSSFWorkbook();ISheetsheet=workbook.CreateSheet("sheet1");ExcelHelper.SetCellDropdownList(sheet,1,1,newList<string>(){"男","女","保密"}.ToArray());第二种

&middot; 通过绑定值到sheet中设置下拉

&middot; 适用于数据较多,或灵活控制的值,例如:城市区域、数据表信息等。

方法块:

publicstaticvoidSetCellDropdownList(HSSFWorkbookworkbook,ISheetsheet,stringname,intfirstcol,intlastcol,string[]vals,intsheetindex=1){//先创建一个Sheet专门用于存储下拉项的值ISheetsheet2=workbook.CreateSheet(name);//隐藏workbook.SetSheetHidden(sheetindex,true);intindex=0;foreach(variteminvals){sheet2.CreateRow(index).CreateCell(0).SetCellValue(item);index++;}//创建的下拉项的区域:varrangeName=name+"Range";INamerange=workbook.CreateName();range.RefersToFormula=name+"!$A$1:$A$"+index;range.NameName=rangeName;CellRangeAddressListregions=newCellRangeAddressList(0,65535,firstcol,lastcol);DVConstraintconstraint=DVConstraint.CreateFormulaListConstraint(rangeName);HSSFDataValidationdataValidate=newHSSFDataValidation(regions,constraint);dataValidate.CreateErrorBox("输入不合法","请输入或选择下拉列表中的值。");dataValidate.ShowPromptBox=true;sheet.AddValidationData(dataValidate);}

调用:

HSSFWorkbookworkbook=newHSSFWorkbook();ISheetsheet=workbook.CreateSheet("sheet1");varroomTypeList=GetRoomTypeNameList();ExcelHelper.SetCellDropdownList(workbook,sheet,"RoomTypeDictionary",1,1,roomTypeList.ToArray());

另外,延伸联动下拉(直接贴源码了)

方法块:

privatevoidSetCityCellDropdownList(HSSFWorkbookworkbook,ISheetsheet,stringdictionaryName,intcitycol,intareacol,intsheetIndex){varcitylist=GetCityList();intcitycount=citylist.Count;ISheetsheet2=workbook.CreateSheet(dictionaryName);//隐藏workbook.SetSheetHidden(sheetIndex,true);#region城市区域数据构造//城市introwIndex=0;foreach(varitemincitylist){IRowrow=sheet2.CreateRow(rowIndex);row.CreateCell(0).SetCellValue(item.Name);rowIndex++;}//区域intn_rowIndex=0;foreach(varitemincitylist){intareaIndex=0;foreach(varareainitem.AreaList){IRowrow=sheet2.GetRow(areaIndex);if(row==null){row=sheet2.CreateRow(areaIndex);}row.CreateCell(n_rowIndex+1).SetCellValue(area.Name);areaIndex++;}n_rowIndex++;}#endregion#region设置数据字段范围//定义城市intcolumnIndex=1;INamerange_Country=workbook.CreateName();range_Country.RefersToFormula=string.Format("{0}!${1}$1:${1}${2}",dictionaryName,GetExcelColumnName(columnIndex),citycount);range_Country.NameName="城市";//定义区foreach(varitemincitylist){intareacount=item.AreaList.Count;columnIndex++;INamerange_area=workbook.CreateName();range_area.RefersToFormula=string.Format("{0}!${1}$1:${1}${2}",dictionaryName,GetExcelColumnName(columnIndex),areacount);range_area.NameName=item.Name;}//城市列表下拉绑定ExcelHelper.SetCellDropdownList(sheet,1,65535,citycol,citycol,"城市");//第二列,跟随第一列联动stringcolName=GetExcelColumnName(areacol);for(intj=1;j<500;j++){ExcelHelper.SetCellDropdownList(sheet,j,j,areacol,areacol,string.Format("INDIRECT(${0}${1})",colName,j+1));}#endregion}privatestringGetExcelColumnName(intcolumnNumber){intdividend=columnNumber;stringcolumnName=String.Empty;intmodulo;while(dividend>0){modulo=(dividend-1)%26;columnName=Convert.ToChar(65+modulo).ToString()+columnName;dividend=(int)((dividend-modulo)/26);}returncolumnName;}

publicstaticvoidSetCellDropdownList(ISheetsheet,intfirstRow,intlastRow,intfirstCol,intlastCol,stringname){CellRangeAddressListregions=newCellRangeAddressList(firstRow,lastRow,firstCol,lastCol);DVConstraintconstraint=DVConstraint.CreateFormulaListConstraint(name);HSSFDataValidationdataValidate=newHSSFDataValidation(regions,constraint);dataValidate.CreateErrorBox("输入不合法","请输入或选择下拉列表中的值。");sheet.AddValidationData(dataValidate);}

调用:

HSSFWorkbookworkbook=newHSSFWorkbook();ISheetsheet=workbook.CreateSheet("sheet1");SetCityCellDropdownList(workbook,sheet,"CityDictionary",1,2,1);

感谢各位的阅读!关于“C#如何使用NPOI设置Excel下拉选项”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!