Code:
1 Public Static voidDropDownList (string[] datas,stringFilePath)2 {3Hssfworkbook Workbook =NewHssfworkbook ();4Isheet sheet = workbook. Createsheet ("Drop- down list test");5Isheet hidden = workbook. Createsheet ("Hidden");6 //Data Source Sheet page does not display7Workbook. Setsheethidden (workbook. Getsheetindex (Hidden),true);8Icellstyle style =workbook. Createcellstyle ();9Style. DataFormat = Hssfdataformat.getbuiltinformat ("0");TenStyle. Alignment =HorizontalAlignment.Center; OneStyle. VerticalAlignment =Verticalalignment.center; AIRow row =NULL; -Icell cell =NULL; - for(inti =0; I < datas. Length; i++) the { -row =hidden. CreateRow (i); -Cell = row. Createcell (0); - cell. Setcellvalue (Datas[i]); + } -Iname Namedcell =workbook. Createname (); +Namedcell.namename ="Hidden"; ANamedcell.referstoformula ="hidden! a$1:a$"+datas. Length; atHssfdatavalidationhelper Dvhelper =NewHssfdatavalidationhelper (sheet ashssfsheet); -Idatavalidationconstraint dvconstraint = (idatavalidationconstraint) dvhelper.createformulalistconstraint ("Hidden"); -Cellrangeaddresslist AddressList =NULL; -hssfdatavalidation validation =NULL; - for(inti =0; I < datas. Length; i++) - { inrow =sheet. CreateRow (i); -Cell = row. Createcell (0); toCell. CellStyle =style; +AddressList =NewCellrangeaddresslist (i, I,0,0); -validation =(hssfdatavalidation) dvhelper.createvalidation (Dvconstraint, AddressList); the sheet. Addvalidationdata (validation); * } $ Panax NotoginsengFileStream stream =NewFileStream (FilePath, filemode.openorcreate); - workbook. Write (stream); the Stream. Close (); +}
View Code
Call:
1 Static voidMain (string[] args)2 {3 intMax = -;4 string[] Datas =New string[Max];5 for(inti =0; i < Max; i++)6 {7Datas[i] =""+i;8 }9 Ten stringFilePath =@"F:\\test.xls"; One dropDownList (datas, filePath); A - Console.read (); -}
Npoi Resolving drop-down list String literals in formulas can ' t be bigger than 255 Chars ASCII