使用带有电子邮件验证的Apache poi生成excel表

时间:2015-06-20 05:17:26

标签: java excel apache-poi

我正在使用apache poi为电子邮件生成excel表 - 这意味着如果用户在单元格中输入电子邮件生成Excel,我需要验证该电子邮件

下面是我的代码

DVConstraint EmailConstraint=null;
DataValidation EmaildataValidation =null;

EmailConstraint=DVConstraint.createCustomFormulaConstraint("AND(FIND("+'@'+",$A$2),FIND(.,$A$2),ISERROR(FIND( ,$A$2)))");---->getting error here only

EmaildataValidation =dataValidationHelper.createValidation(EmailConstraint, addressList1);
       EmaildataValidation.setSuppressDropDownArrow(false);
       sheet.addValidationData(EmaildataValidation);

当我执行此方法时,我收到以下错误

Stacktrace:] with root cause
org.apache.poi.ss.formula.FormulaParseException: Parse error near char 9 '@' in specified formula 'AND(FIND(@,$A$2),FIND(.,$A$2),ISERROR(FIND( ,$A$2)))'. Expected cell ref or constant literal
    at org.apache.poi.ss.formula.FormulaParser.expected(FormulaParser.java:188)
    at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1128)
    at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1083)
    at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1070)
    at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1430)
    at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1530)
    at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1514)
    at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1471)
    at org.apache.poi.ss.formula.FormulaParser.Arguments(FormulaParser.java:1056)
    at org.apache.poi.ss.formula.FormulaParser.function(FormulaParser.java:943)
    at org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:536)
    at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:416)
    at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:237)
    at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1123)
    at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1083)
    at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1070)
    at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1430)
    at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1530)
    at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1514)
    at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1471)
    at org.apache.poi.ss.formula.FormulaParser.Arguments(FormulaParser.java:1056)
    at org.apache.poi.ss.formula.FormulaParser.function(FormulaParser.java:943)
    at org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:536)
    at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:416)
    at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:237)
    at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1123)
    at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1083)
    at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1070)

我尝试了很多但却无法做到。

1 个答案:

答案 0 :(得分:2)

查找函数的第一个参数是字符串,因此必须引用它。 查找(" @" .. 在java中,您可以在公式之间使用转义字符串字符。