【问题标题】:How can I parse an excel formula?如何解析excel公式?
【发布时间】:2016-07-12 22:16:08
【问题描述】:

我正在将包含在复杂 Excel 工作表中的功能转换为 ASP.Net 项目。在此,我需要使用 VB.NET 或 c# 解析/处理类似公式的 excel。

我有一个类似网格的结构,可以显示会计数据,并且允许用户在所需的单元格中预先配置公式。

示例:- 在我的数据网格 Cell[2][1] 中,我应该能够配置

公式 = Sum(Cell[1][1] + Cell[1][2]) .

有没有一种方法可以解析/处理来自 vb.net/c# 的 excel 公式?

【问题讨论】:

标签: c# vb.net excel


【解决方案1】:

您可以使用 FLEE 库来评估类似 C# 的表达式

// Create the calculation engine
CalculationEngine engine = new CalculationEngine();
ExpressionContext context = new ExpressionContext();
VariableCollection variables = context.Variables;

// Add some variables
variables.Add("x", 100);            
variables.Add("y", 200);

// Add an expression to the calculation engine as "a"
engine.Add("a", "x * 2", context);

// Add an expression to the engine as "b"
engine.Add("b", "y + 100", context);

// Add an expression at "c" that uses the results of "a" and "b"
engine.Add("c", "a + b", context);

// Get the value of "c"
int result = engine.GetResult<int>("c");

// Update a variable on the "a" expression            
variables["x"] = 200;

// Recalculate it
engine.Recalculate("a");

// Get the updated result
result = engine.GetResult<int>("c");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    相关资源
    最近更新 更多