Excel导出下拉框(导出下拉框较多的选项)

二、导出下拉框较多的选项

使用常规下拉框的导出,会出现超过255字段异常的问题,可以使用引用隐藏的sheet来解决

#region 设置下拉框
if (dicDropList != null)
{
int bookIdenx = 1;
foreach (var item in dicDropList)
{
if (item.Value == null || item.Value.Rows.Count == 0)
{
continue;
}
ISheet sheet2 = workbook.CreateSheet("ShtDictionary" + item.Key);
for (int j = 0; j < item.Value.Rows.Count; j++)
{
sheet2.CreateRow(j).CreateCell(0).SetCellValue(item.Value.Rows[j][0].ToString());
}
IName range = workbook.CreateName();
range.RefersToFormula = "ShtDictionary" + item.Key + "!$A$1:$A$" + item.Value.Rows.Count;    //这里引用位置不用错了 "!$A$1:$A$" + item.Value.Rows.Count
range.NameName = "dicRange" + item.Key;

CellRangeAddressList regions = new CellRangeAddressList(2, 65535, item.Key, item.Key);
DVConstraint constraint = DVConstraint.CreateFormulaListConstraint("dicRange" + item.Key);
HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
dataValidate.CreateErrorBox("输入不合法", "请输入下拉列表中的值。");
sheet.AddValidationData(dataValidate);
workbook.SetSheetHidden(bookIdenx, true);
bookIdenx += 1;
}
}
#endregion

猜你喜欢

转载自www.cnblogs.com/zhusk/p/10524986.html