Npoi 导出Excel 下拉列表异常: String literals in formulas can't be bigger than 255 Chars ASCII...

代码:

 1 public static void dropDownList(string[] datas, string filePath)
 2 {
 3     HSSFWorkbook workbook = new HSSFWorkbook();
 4     ISheet sheet = workbook.CreateSheet("下拉列表测试");
 5     ISheet hidden = workbook.CreateSheet("hidden");
 6     //数据源sheet页不显示
 7      workbook.SetSheetHidden(workbook.GetSheetIndex(hidden), true);
 8     ICellStyle style = workbook.CreateCellStyle();
 9     style.DataFormat = HSSFDataFormat.GetBuiltinFormat("0");
10     style.Alignment = HorizontalAlignment.Center;
11     style.VerticalAlignment = VerticalAlignment.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

调用:

 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 }

 

转载于:https://www.cnblogs.com/zlen/p/5673146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值