Spreadsheetgear 用它的结果替换公式

Spreadsheetgear replace formula with it's result

我将 spreadsheetgear 2012 用于 c# .net excel 操作目的。我在某些单元格中有一些 excel 公式。现在我想用评估的数字替换 excel 中的公式。有什么特别的方法可以做到这一点吗?尝试搜索 spreadsheetgear 论坛但没有帮助。

这可以通过几种不同的方式完成:

  • 设置 IRange。Value to itself for the desired range. IRange.Formula contains a cell's formula and IRange.Value the calculated value. Setting IRange.Value to itself will effectively remove the formula from the cell and leave only the value. This will work on a single cell and also on a multi-cell range (since IRange.Value 可以 return 对象 [] 数组,也可以从对象 [] 数组设置。
  • 在所需范围上使用 IRange.Copy(...) method with the PasteType.Values 选项,这相当于在 Microsoft Excel.
  • 中执行选择性粘贴 > 值

以下是这些方法的示例:

using SpreadsheetGear;
...

// Create workbook and some local variables.
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.ActiveWorksheet;
IRange cells = worksheet.Cells;

// EXAMPLE 1 - IRange.Value with single cell
// Set A1 to a formula and output some info about A1's contents.
IRange a1 = cells["A1"];
a1.Formula = "=ROUND(RAND(),2)";
Console.WriteLine($"{a1.Address}: HasFormula={a1.HasFormula}, Formula='{a1.Formula}', Value={a1.Value}");
// OUTPUT: $A: HasFormula=True, Formula='=ROUND(RAND(),2)', Value=0.69
// Set IRange.Value to itself
a1.Value = a1.Value;
Console.WriteLine($"{a1.Address}: HasFormula={a1.HasFormula}, Formula='{a1.Formula}', Value={a1.Value}");
// OUTPUT: $A: HasFormula=False, Formula='0.69', Value=0.69


// EXAMPLE 2 - IRange.Value with multiple cells
// Works on multiple cells since IRange.Value will return an object[,]
// array for a range representing multiple cells.
IRange b1b3 = cells["B1:B3"];
b1b3.Formula = "=ROUND(RAND(),2)";
foreach (IRange cell in b1b3)
    Console.WriteLine($"{cell.Address}: HasFormula={cell.HasFormula}, Formula='{cell.Formula}', Value={cell.Value}");
// OUTPUT:
// $B: HasFormula=True, Formula='=ROUND(RAND(),2)', Value=0.81
// $B: HasFormula=True, Formula='=ROUND(RAND(),2)', Value=0.55
// $B: HasFormula=True, Formula='=ROUND(RAND(),2)', Value=0.24

// Set IRange.Value to itself (2D object[,] used)
b1b3.Value = b1b3.Value;
foreach (IRange cell in b1b3)
    Console.WriteLine($"{cell.Address}: HasFormula={cell.HasFormula}, Formula='{cell.Formula}', Value={cell.Value}");
// OUTPUT:
// $B: HasFormula=False, Formula='0.81', Value=0.81
// $B: HasFormula=False, Formula='0.55', Value=0.55
// $B: HasFormula=False, Formula='0.24', Value=0.24


// EXAMPLE 3 - IRange.Copy() with PasteType.Values option
IRange c1c2 = cells["C1:C2"];
c1c2.Formula = "=ROUND(RAND(),2)";
foreach (IRange cell in c1c2)
    Console.WriteLine($"{cell.Address}: HasFormula={cell.HasFormula}, Formula='{cell.Formula}', Value={cell.Value}");
// OUTPUT:
// $C: HasFormula=True, Formula='=ROUND(RAND(),2)', Value=0.04
// $C: HasFormula=True, Formula='=ROUND(RAND(),2)', Value=0.14

c1c2.Copy(c1c2, PasteType.Values, PasteOperation.None, false, false);
foreach (IRange cell in c1c2)
    Console.WriteLine($"{cell.Address}: HasFormula={cell.HasFormula}, Formula='{cell.Formula}', Value={cell.Value}");
// OUTPUT:
// $C: HasFormula=False, Formula='0.04', Value=0.04
// $C: HasFormula=False, Formula='0.14', Value=0.14