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

时间:2024-08-24 17:05:26

代码:

 public static void dropDownList(string[] datas, string filePath)
{
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet("下拉列表测试");
ISheet hidden = workbook.CreateSheet("hidden");
//数据源sheet页不显示
workbook.SetSheetHidden(workbook.GetSheetIndex(hidden), true);
ICellStyle style = workbook.CreateCellStyle();
style.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
style.Alignment = HorizontalAlignment.Center;
style.VerticalAlignment = VerticalAlignment.Center;
IRow row = null;
ICell cell = null;
for (int i = ; i < datas.Length; i++)
{
row = hidden.CreateRow(i);
cell = row.CreateCell();
cell.SetCellValue(datas[i]);
}
IName namedCell = workbook.CreateName();
namedCell.NameName = "hidden";
namedCell.RefersToFormula = "hidden!A$1:A$" + datas.Length;
HSSFDataValidationHelper dvHelper = new HSSFDataValidationHelper(sheet as HSSFSheet);
IDataValidationConstraint dvConstraint = (IDataValidationConstraint)dvHelper.CreateFormulaListConstraint("hidden");
CellRangeAddressList addressList = null;
HSSFDataValidation validation = null;
for (int i = ; i < datas.Length; i++)
{
row = sheet.CreateRow(i);
cell = row.CreateCell();
cell.CellStyle = style;
addressList = new CellRangeAddressList(i, i, , );
validation = (HSSFDataValidation)dvHelper.CreateValidation(dvConstraint, addressList);
sheet.AddValidationData(validation);
} FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate);
workbook.Write(stream);
stream.Close();
}

调用:

 static void Main(string[] args)
{
int max = ;
string[] datas = new string[max];
for (int i = ; i < max; i++)
{
datas[i] = "" + i;
} string filePath = @"F:\\test.xls";
dropDownList(datas, filePath); Console.Read();
}