C#使用NPOI設(shè)置Excel下拉選項(xiàng)_第1頁
C#使用NPOI設(shè)置Excel下拉選項(xiàng)_第2頁
C#使用NPOI設(shè)置Excel下拉選項(xiàng)_第3頁
C#使用NPOI設(shè)置Excel下拉選項(xiàng)_第4頁
C#使用NPOI設(shè)置Excel下拉選項(xiàng)_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論