C#使用NPOI設置Excel下拉選項
本文實例為大傢分享瞭C#使用NPOI設置Excel下拉選項的具體代碼,供大傢參考,具體內容如下
最近在做一個導出模板的功能,需要限制用戶的某些單元格輸入的內容。
期望達到的效果:單元格中出現下拉選擇,或輸入錯誤時提示。
翻閱瞭許多資料,終於得到瞭答案。
然後自己整理下邊一些方法,記錄下來方便以後使用。
第一種
· 直接設置下拉值,不超過255個字符(優點:邏輯簡單 ;缺點:有字符限制)
· 適用於下拉值為固定值,例如:狀態、性別等
方法塊:
public static void SetCellDropdownList(ISheet sheet, int firstcol, int lastcol, string[] vals) { //設置生成下拉框的行和列 var cellRegions = new CellRangeAddressList(1, 65535, firstcol, lastcol); //設置 下拉框內容 DVConstraint constraint = DVConstraint.CreateExplicitListConstraint(vals); //綁定下拉框和作用區域,並設置錯誤提示信息 HSSFDataValidation dataValidate = new HSSFDataValidation(cellRegions, constraint); dataValidate.CreateErrorBox("輸入不合法", "請輸入或選擇下拉列表中的值。"); dataValidate.ShowPromptBox = true; sheet.AddValidationData(dataValidate); }
調用:
HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("sheet1"); ExcelHelper.SetCellDropdownList(sheet, 1, 1, new List<string>() { "男", "女", "保密" }.ToArray());
第二種
· 通過綁定值到sheet中設置下拉
· 適用於數據較多,或靈活控制的值,例如:城市區域、數據表信息等。
方法塊:
public static void SetCellDropdownList(HSSFWorkbook workbook, ISheet sheet, string name, int firstcol, int lastcol, string[] vals, int sheetindex = 1) { //先創建一個Sheet專門用於存儲下拉項的值 ISheet sheet2 = workbook.CreateSheet(name); //隱藏 workbook.SetSheetHidden(sheetindex, true); int index = 0; foreach (var item in vals) { sheet2.CreateRow(index).CreateCell(0).SetCellValue(item); index++; } //創建的下拉項的區域: var rangeName = name + "Range"; IName range = workbook.CreateName(); range.RefersToFormula = name + "!$A$1:$A$" + index; range.NameName = rangeName; CellRangeAddressList regions = new CellRangeAddressList(0, 65535, firstcol, lastcol); DVConstraint constraint = DVConstraint.CreateFormulaListConstraint(rangeName); HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint); dataValidate.CreateErrorBox("輸入不合法", "請輸入或選擇下拉列表中的值。"); dataValidate.ShowPromptBox = true; sheet.AddValidationData(dataValidate); }
調用:
HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("sheet1"); var roomTypeList = GetRoomTypeNameList(); ExcelHelper.SetCellDropdownList(workbook, sheet, "RoomTypeDictionary", 1, 1, roomTypeList.ToArray());
另外,延伸聯動下拉(直接貼源碼瞭)
方法塊:
private void SetCityCellDropdownList(HSSFWorkbook workbook, ISheet sheet, string dictionaryName, int citycol, int areacol, int sheetIndex) { var citylist = GetCityList(); int citycount = citylist.Count; ISheet sheet2 = workbook.CreateSheet(dictionaryName); //隱藏 workbook.SetSheetHidden(sheetIndex, true); #region 城市區域數據構造 //城市 int rowIndex = 0; foreach (var item in citylist) { IRow row = sheet2.CreateRow(rowIndex); row.CreateCell(0).SetCellValue(item.Name); rowIndex++; } //區域 int n_rowIndex = 0; foreach (var item in citylist) { int areaIndex = 0; foreach (var area in item.AreaList) { IRow row = sheet2.GetRow(areaIndex); if (row == null) { row = sheet2.CreateRow(areaIndex); } row.CreateCell(n_rowIndex + 1).SetCellValue(area.Name); areaIndex++; } n_rowIndex++; } #endregion #region 設置數據字段范圍 //定義城市 int columnIndex = 1; IName range_Country = workbook.CreateName(); range_Country.RefersToFormula = string.Format("{0}!${1}$1:${1}${2}", dictionaryName, GetExcelColumnName(columnIndex), citycount); range_Country.NameName = "城市"; //定義區 foreach (var item in citylist) { int areacount = item.AreaList.Count; columnIndex++; IName range_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, "城市"); //第二列,跟隨第一列聯動 string colName = GetExcelColumnName(areacol); for (int j = 1; j < 500; j++) { ExcelHelper.SetCellDropdownList(sheet, j, j, areacol, areacol, string.Format("INDIRECT(${0}${1})", colName, j + 1)); } #endregion } private string GetExcelColumnName(int columnNumber) { int dividend = columnNumber; string columnName = String.Empty; int modulo; while (dividend > 0) { modulo = (dividend - 1) % 26; columnName = Convert.ToChar(65 + modulo).ToString() + columnName; dividend = (int)((dividend - modulo) / 26); } return columnName; }
public static void SetCellDropdownList(ISheet sheet, int firstRow, int lastRow, int firstCol, int lastCol, string name) { CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); DVConstraint constraint = DVConstraint.CreateFormulaListConstraint(name); HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint); dataValidate.CreateErrorBox("輸入不合法", "請輸入或選擇下拉列表中的值。"); sheet.AddValidationData(dataValidate); }
調用:
HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("sheet1"); SetCityCellDropdownList(workbook, sheet, "CityDictionary", 1, 2, 1);
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- C#通過NPOI導入導出數據EXCEL
- C#使用NPOI對Excel數據進行導入導出
- C#使用NPOI讀取excel轉為DataSet
- C#使用Npoi導出Excel並合並行列
- C#使用NPOI將excel導入到list的方法