【问题标题】:npoi SetCellFormula custom formula in VBAVBA中的npoi SetCellFormula自定义公式
【发布时间】:2014-09-24 11:12:34
【问题描述】:

我有一个填充数据的应用程序,一个 excel 模板。模板是 .xlsm。在模板中,我创建了名为 SumByColor 的 vba 函数,当我尝试为该函数设置单元格公式时出现此错误:

{"Name 'SumByColor' is completely unknown in the current workbook"}

我这样设置公式:

sheet.GetRow(rowIndex).GetCell(startPos + 2).SetCellFormula(string.Format("SumByColor($AQ$7,F{0}:AI{0})",rowIndex+1));

【问题讨论】:

  • 你应该设置为"=SumByColor(...)"吗?
  • 不,如果我这样写 "=SumByColor(...)" 我得到:{NPOI.SS.Formula.FormulaParseException: The specified formula '=SumByColor($AQ$7,F8:AI8)'以不允许的等号开头。
  • 这很奇怪。当您不使用 equal 时,它似乎认为 SumByColor 是一个范围名称,而当您使用 equal 时,它不会让您这样做。当您说您have an application 时,您指的是什么类型的应用程序?您如何通过应用程序与 Excel 进行通信?

标签: c# vba excel npoi


【解决方案1】:

在为单元格设置公式之前,您是否尝试过将单元格类型设置为公式。例如:

XSSFRow row = (XSSFRow)sheet.GetRow(i);
ICell cell = row.CreateCell(1);  
cell.SetCellType(CellType.Formula);
cell.SetCellFormula("SUM(B1:B10)");

您可以阅读有关设置公式和约束的更多信息。

【讨论】:

    【解决方案2】:

    你是如何分配公式的? 你在尝试这样的事情吗?

    Range("A10") = "=SumByColor($AQ$7,F8:AI8)"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多