例外:DataValidation列表的总长度不能超过255个字符

我试图在epplus中动态创建一个公式字段。 如果公式字段包含少于255个字符,那么它正在正确创建。 如果它超过255,那么它会抛出exception,因为Exception:DataValidation列表的总长度不能超过255个字符。

任何人都可以帮我解决这个问题吗? 或者请告诉我一些替代方案。

问题是您正在使用该单元格的Formula容器来存储所有可用的列表选项 – 基本上是CSV列表。 Excel中的硬限制为255个字符。 您可以通过进入excel并在创建新的validation列表时手动输入“源”框中的逗号分隔的值来查看此信息。

您最好的选择可能是填充单元格中的值,并将值的范围赋予公式。 像这样:

 using (var pack = new ExcelPackage(existingFile)) { var ws = pack.Workbook.Worksheets.Add("Content"); //var val = ws.DataValidations.AddListValidation("A1"); //val.Formula.Values.Add("Here we have to add long text"); //val.Formula.Values.Add("All list values combined have to have more then 255 chars"); //val.Formula.Values.Add("more text 1 more text more text more text"); //val.Formula.Values.Add("more text 2 more text more text more text"); ws.Cells["B1"].Value = "Here we have to add long text"; ws.Cells["B2"].Value = "All list values combined have to have more then 255 chars"; ws.Cells["B3"].Value = "more text 1 more text more text more text"; ws.Cells["B4"].Value = "more text 2 more text more text more text"; ws.Cells["B5"].Value = "more text 2 more text more text more textmore text 2 more text more text more textmore text 2 more text more text more textmore text 2 more text more text more textmore text 2 more text more text more textmore text 2 more text more text more textmore text 2 more text more text more textmore"; var val = ws.DataValidations.AddListValidation("A1"); val.Formula.ExcelFormula = "B1:B5"; pack.SaveAs(existingFile); } 

厄尼的解决方案完美无缺! 除了每行不断变化的值范围。 (即第一行从B1添加项目:B5,第二行B2:B6 …)

此代码更改解决了以下问题:

 val.Formula.ExcelFormula = "$B$1:$B$5"; 

[添加作为解决方案,因为我不被评论:)]