异常:DataValidation列表的总长度不能超过255个字符。

7

我正在尝试在epplus中动态创建公式字段。如果公式字段的字符数少于255个,则可以正确创建。如果超过255个,则会抛出异常,如下所示:

异常:数据验证列表的总长度不能超过255个字符。

请问有人能帮我解决这个问题吗?或者请告诉我一些替代方案。


如果答案不够充分,请提供关于您目前所拥有的更多信息。 - Nick
嗨,尼古拉斯,感谢您的回复。我正在尝试类似于这样的操作,但是我遇到了上述异常。我不想限制公式字段。我想在公式字段中添加超过255个字符的内容。' 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"); pack.SaveAs(new FileInfo("err.xlsx"));' - Keerthi Kumar
2个回答

12

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

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

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);
}

3

Ernie的解决方案非常完美!除了值的范围随着每一行的变化而变化。(例如,第一行添加了B1:B5中的项目,第二行添加了B2:B6...)

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

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

[因为没有被允许评论,所以将其作为解决方案添加 :) ]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接