添加 Cell 公式

将公式添加到 Cells

如何添加和计算公式?

可以使用单元格的公式属性在单元格中添加、访问和修改公式。 Aspose.Cells.GridWeb支持从简单到复杂的用户自定义公式。但是,Aspose.Cells.GridWeb也提供了大量的内置函数或公式(类似于Microsoft Excel)。要查看内置函数的完整列表,请参阅此支持的功能列表。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the worksheet of the Grid that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Putting some values to cells
sheet.Cells["A1"].PutValue("1st Value");
sheet.Cells["A2"].PutValue("2nd Value");
sheet.Cells["A3"].PutValue("Sum");
sheet.Cells["B1"].PutValue(125.56);
sheet.Cells["B2"].PutValue(23.93);
// Adding a simple formula to "B3" cell
sheet.Cells["B3"].Formula = "=SUM(B1:B2)";

公式添加到 B3 单元格但未由 GridWeb 计算

待办事项:图片_替代_文本

在上面的截图中,可以看到B3中已经添加了一个公式,但是还没有进行计算。要计算所有公式,请在将公式添加到工作表后调用 GridWeb 控件的 GridWorksheetCollection 的 CalculateFormula 方法,如下所示。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Calculating all formulas added in worksheets
GridWeb1.WorkSheets.CalculateFormula();

从其他工作表中引用 Cells

使用 Aspose.Cells.GridWeb,可以在其公式中引用存储在不同工作表中的值,从而创建复杂的公式。

从不同工作表引用单元格值的语法是 SheetName!CellName。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Adding a bit complex formula to "A1" cell
sheet1.Cells["B6"].Formula = "=(SUM(A1:A5)/AVERAGE(B1:B5))-Sheet2!B1";