版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第C#使用NPOI設(shè)置Excel下拉選項(xiàng)本文實(shí)例為大家分享了C#使用NPOI設(shè)置Excel下拉選項(xiàng)的具體代碼,供大家參考,具體內(nèi)容如下
最近在做一個(gè)導(dǎo)出模板的功能,需要限制用戶的某些單元格輸入的內(nèi)容。
期望達(dá)到的效果:單元格中出現(xiàn)下拉選擇,或輸入錯(cuò)誤時(shí)提示。
翻閱了許多資料,終于得到了答案。
然后自己整理下邊一些方法,記錄下來方便以后使用。
直接設(shè)置下拉值,不超過255個(gè)字符(優(yōu)點(diǎn):邏輯簡單;缺點(diǎn):有字符限制)
適用于下拉值為固定值,例如:狀態(tài)、性別等
方法塊:
publicstaticvoidSetCellDropdownList(ISheetsheet,intfirstcol,intlastcol,string[]vals)
//設(shè)置生成下拉框的行和列
varcellRegions=newCellRangeAddressList(1,65535,firstcol,lastcol);
//設(shè)置下拉框內(nèi)容
DVConstraintconstraint=DVConstraint.CreateExplicitListConstraint(vals);
//綁定下拉框和作用區(qū)域,并設(shè)置錯(cuò)誤提示信息
HSSFDataValidationdataValidate=newHSSFDataValidation(cellRegions,constraint);
dataValidate.CreateErrorBox("輸入不合法","請輸入或選擇下拉列表中的值。");
dataValidate.ShowPromptBox=true;
sheet.AddValidationData(dataValidate);
}
調(diào)用:
HSSFWorkbookworkbook=newHSSFWorkbook();
ISheetsheet=workbook.CreateSheet("sheet1");
ExcelHelper.SetCellDropdownList(sheet,1,1,newListstring(){"男","女","保密"}.ToArray());
通過綁定值到sheet中設(shè)置下拉
適用于數(shù)據(jù)較多,或靈活控制的值,例如:城市區(qū)域、數(shù)據(jù)表信息等。
方法塊:
publicstaticvoidSetCellDropdownList(HSSFWorkbookworkbook,ISheetsheet,stringname,intfirstcol,intlastcol,string[]vals,intsheetindex=1)
//先創(chuàng)建一個(gè)Sheet專門用于存儲下拉項(xiàng)的值
ISheetsheet2=workbook.CreateSheet(name);
//隱藏
workbook.SetSheetHidden(sheetindex,true);
intindex=0;
foreach(variteminvals)
{
sheet2.CreateRow(index).CreateCell(0).SetCellValue(item);
index++;
}
//創(chuàng)建的下拉項(xiàng)的區(qū)域:
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);
}
調(diào)用:
HSSFWorkbookworkbook=newHSSFWorkbook();
ISheetsheet=workbook.CreateSheet("sheet1");
varroomTypeList=GetRoomTypeNameList();
ExcelHelper.SetCellDropdownList(workbook,sheet,"RoomTypeDictionary",1,1,roomTypeList.ToArray());
另外,延伸聯(lián)動(dòng)下拉(直接貼源碼了)
方法塊:
privatevoidSetCityCellDropdownList(HSSFWorkbookworkbook,ISheetsheet,stringdictionaryName,intcitycol,intareacol,intsheetIndex)
varcitylist=GetCityList();
intcitycount=citylist.Count;
ISheetsheet2=workbook.CreateSheet(dictionaryName);
//隱藏
workbook.SetSheetHidden(sheetIndex,true);
#region城市區(qū)域數(shù)據(jù)構(gòu)造
//城市
introwIndex=0;
foreach(varitemincitylist)
{
IRowrow=sheet2.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue(item.Name);
rowIndex++;
}
//區(qū)域
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設(shè)置數(shù)據(jù)字段范圍
//定義城市
intcolumnIndex=1;
INamerange_Country=workbook.CreateName();
range_Country.RefersToFormula=string.Format("{0}!${1}$1:${1}${2}",dictionaryName,GetExcelColumnName(columnIndex),citycount);
range_Country.NameName="城市";
//定義區(qū)
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,"城市");
//第二列,跟隨第一列聯(lián)動(dòng)
stringcolName=GetExcelColumnName(areacol);
for(intj=1;j500;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(dividend0)
{
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("輸入不合法"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026安徽馬鞍山市花山區(qū)屬國有企業(yè)區(qū)內(nèi)選聘25人備考題庫及答案詳解(易錯(cuò)題)
- 黑龍江2025下半年黑龍江省商務(wù)學(xué)校事業(yè)單位招聘16人筆試歷年參考題庫附帶答案詳解
- 銅仁2025年貴州銅仁市松桃縣選調(diào)專任教師160人筆試歷年參考題庫附帶答案詳解
- 邯鄲2025年河北邯鄲館陶縣博碩引才(24人)筆試歷年參考題庫附帶答案詳解
- 贛州2025年江西贛南科技學(xué)院急缺師資招聘筆試歷年參考題庫附帶答案詳解
- 舟山2025年浙江舟山市面向殘疾人招聘事業(yè)單位工作人員筆試歷年參考題庫附帶答案詳解
- 湖南2025年湖南省商業(yè)技師學(xué)院招聘12人筆試歷年參考題庫附帶答案詳解
- 洛陽2025年河南省洛陽市伊川縣引進(jìn)研究生學(xué)歷人才25人筆試歷年參考題庫附帶答案詳解
- 永州2025年湖南永州市東安縣事業(yè)單位招聘72人筆試歷年參考題庫附帶答案詳解
- 威海2025年山東威海干部學(xué)院招聘高層次急需緊缺人才2人筆試歷年參考題庫附帶答案詳解
- 箱涵預(yù)制、安裝、現(xiàn)澆施工方案
- 2026屆杭州高級中學(xué)高二上數(shù)學(xué)期末聯(lián)考試題含解析
- 2026年及未來5年中國無取向硅鋼片行業(yè)市場深度分析及發(fā)展趨勢預(yù)測報(bào)告
- 棄土場規(guī)范規(guī)章制度
- 2026年水下機(jī)器人勘探報(bào)告及未來五至十年深海資源報(bào)告
- 安徽省蕪湖市鳩江區(qū)2024-2025學(xué)年高一上學(xué)期期末考試生物試卷
- 2025年3月29日事業(yè)單位聯(lián)考(職測+綜應(yīng))ABCDE類筆試真題及答案解析
- 2025年對中國汽車行業(yè)深度變革的觀察與思考報(bào)告
- 雙重預(yù)防體系建設(shè)自評報(bào)告模板
- 福建省泉州市晉江市2024-2025學(xué)年八年級上學(xué)期1月期末考試英語試題(含答案無聽力音頻及原文)
- 心血管疾病風(fēng)險(xiǎn)評估
評論
0/150
提交評論