Npoi solution drop-down list String literals in formulas can't be bigger than 255 Chars ASCII, npoiliterals
Code:
1 public static void dropDownList (string [] datas, string filePath) 2 {3 HSSFWorkbook workbook = new HSSFWorkbook (); 4 ISheet sheet = workbook. createSheet ("drop-down list test"); 5 ISheet hidden = workbook. createSheet ("hidden"); 6 // the data source sheet page does not display 7 workbooks. setSheetHidden (workbook. getSheetIndex (hidden), true); 8 ICellStyle style = workbook. createCellStyle (); 9 style. dataFormat = HSSFDataFormat. getBuiltinFormat (" 0 "); 10 style. alignment = HorizontalAlignment. center; 11 style. verticalAlignment = verticalignment. center; 12 IRow row = null; 13 ICell cell = null; 14 for (int I = 0; I <datas. length; I ++) 15 {16 row = hidden. createRow (I); 17 cell = row. createCell (0); 18 cell. setCellValue (datas [I]); 19} 20 IName namedCell = workbook. createName (); 21 namedCell. nameName = "hidden"; 22 namedCell. refersToFormula = "hidden! A $1: A $ "+ datas. length; 23 HSSFDataValidationHelper dvHelper = new HSSFDataValidationHelper (sheet as HSSFSheet); 24 IDataValidationConstraint dvConstraint = (IDataValidationConstraint) dvHelper. createFormulaListConstraint ("hidden"); 25 CellRangeAddressList addressList = null; 26 HSSFDataValidation validation = null; 27 for (int I = 0; I <datas. length; I ++) 28 {29 row = sheet. createRow (I); 30 cell = row. createCell (0); 31 cell. cellStyle = style; 32 addressList = new CellRangeAddressList (I, I, 0, 0); 33 validation = (HSSFDataValidation) dvHelper. createValidation (dvConstraint, addressList); 34 sheet. addValidationData (validation); 35} 36 37 FileStream stream = new FileStream (filePath, FileMode. openOrCreate); 38 workbook. write (stream); 39 stream. close (); 40}View Code
Call:
1 static void Main(string[] args) 2 { 3 int max = 100; 4 string[] datas = new string[max]; 5 for (int i = 0; i < max; i++) 6 { 7 datas[i] = "" + i; 8 } 9 10 string filePath = @"F:\\test.xls";11 dropDownList(datas, filePath);12 13 Console.Read();14 }