Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 例外:数据验证列表的总长度不能超过255个字符_C#_Asp.net_Epplus - Fatal编程技术网

C# 例外:数据验证列表的总长度不能超过255个字符

C# 例外:数据验证列表的总长度不能超过255个字符,c#,asp.net,epplus,C#,Asp.net,Epplus,我正在尝试在epplus中动态创建公式字段。如果“公式”字段包含少于255个字符,则该字段创建正确。如果超过255 然后它抛出一个异常作为 异常:数据验证列表的总长度不能超过255个字符。 谁能帮我解决这个问题?或者请告诉我一些备选方案。问题是您正在使用该单元格的公式容器来存储所有可用的列表选项-基本上是CSV列表。在Excel中有255个字符的硬限制。创建新的验证列表时,您可以通过进入excel并在“源”框中手动输入以逗号分隔的值来看到这一点 最好的选择可能是在单元格中填充值,并为公式指定值的

我正在尝试在epplus中动态创建公式字段。如果“公式”字段包含少于255个字符,则该字段创建正确。如果超过255 然后它抛出一个异常作为 异常:数据验证列表的总长度不能超过255个字符。


谁能帮我解决这个问题?或者请告诉我一些备选方案。

问题是您正在使用该单元格的
公式
容器来存储所有可用的列表选项-基本上是CSV列表。在Excel中有255个字符的硬限制。创建新的验证列表时,您可以通过进入excel并在“源”框中手动输入以逗号分隔的值来看到这一点

最好的选择可能是在单元格中填充值,并为公式指定值的范围。像这样:

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";

[添加作为解决方案,因为我不允许发表评论:)]

如果答案不充分,请提供有关您当前拥有的内容的其他信息。嗨,Nicholas,谢谢您的回复。我尝试类似于此,但遇到上述异常,我不想限制公式字段。我想在公式字段“var val=ws.DataValidations.AddListValidation”(“A1”)中添加超过255个字符;Add(“这里我们必须添加长文本”);val.Formula.Values.Add(“所有组合的列表值必须超过255个字符”);val.Formula.Values.Add(“更多文本1更多文本更多文本”);val.Formula.Values.Add(“更多文本2更多文本更多文本”);pack.SaveAs(新文件信息(“err.xlsx”);'