1

这是示例代码

XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;
XL.Range range = worksheet.UsedRange;
arrValue = (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);

for (int iRow = 1; iRow <= arrValue.GetLength(0); iRow++)
{
    for (int iCol = 1; iCol <= arrValue.GetLength(1); iCol++)
    {
         if (range.Cells[iRow, iCol].Formula != null)
              range.Cells[iRow, iCol].Formula = "=" + kvp.Value.ToString();  // assign underlying formula (kvp is keyValuePair of dictionary) to the cell

         range.Cells[iRow, iCol].Value = evalResults[count].ToString(); // assign value to the cell 
         count++;
    }

} 

我想知道 Cells.Value 和 Cells.Formula 是否会相互覆盖。如果我想同时分配它们,最好的方法是什么?

谢谢。

4

2 回答 2

3

无法将值和公式分配给 excel 单元格(即使以正常的交互方式使用 excel,您也不能这样做),因为结果是未定义的,即如果公式的结果不同于你会在单元格中显示什么值?

由于您想保留公式以供将来重用,您可以使用 Recalculate 方法应用公式,将它们保存到字典中(并且您已经有了类似的东西,不是吗?),然后设置具有当前值的单元格(根据公式计算)。
也许这就是你已经在这里做的,但不是很清楚,因为我不明白 evalResults 是什么。无论如何,我的意思是这样的:

private Dictionary<int,Dictionary<int,string>> formulas = new Dictionary<int,Dictionary<int,string>>(); // this has to be initialized according to your formulas
public void Recalculate()
{
    XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;
    XL.Range range = worksheet.UsedRange;

    for (int iRow = 1; iRow <= arrValue.GetLength(0); iRow++)
    {
        for (int iCol = 1; iCol <= arrValue.GetLength(1); iCol++)
        {
            range.Cells[iRow, iCol].Formula = "=" + formulas[iRow][iCol];  
            range.Cells[iRow, iCol].Value = range.Cells[iRow, iCol].Value; 
        }

    }
}

如果您想将公式保留在 excel 工作簿中而不是内存中(在字典中),一个解决方案可能是有一个隐藏的工作表,您可以在其中将公式存储为字符串(即没有“=”)以避免它们评估和绩效问题。因此,您将拥有与您的代码非常相似的 SetFormulas 和 Recalculate 方法:

public void SetFormulas()
{
    XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;
    XL.Worksheet formulasWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets["formulas"];
    XL.Range range = worksheet.UsedRange;
    arrValue = (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);

    for (int iRow = 1; iRow <= arrValue.GetLength(0); iRow++)
    {
        for (int iCol = 1; iCol <= arrValue.GetLength(1); iCol++)
        {
             if (range.Cells[iRow, iCol].Formula != null)
             {
                  range.Cells[iRow, iCol].Formula = "=" + kvp.Value.ToString();  // assign underlying formula (kvp is keyValuePair of dictionary) to the cell
                  formulasWorksheet.Cells[iRow, iCol].Value = kvp.Value.ToString(); // storing the formula here
             }
             range.Cells[iRow, iCol].Value = evalResults[count].ToString(); // assign value to the cell 
             count++;
        }

    }
}



public void Recalculate()
{
    XL.Worksheet worksheet = Globals.ThisAddIn.Application.ActiveSheet;
    XL.Worksheet formulasWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets["formulas"];
    XL.Range range = worksheet.UsedRange;
    arrValue = (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault);

    for (int iRow = 1; iRow <= arrValue.GetLength(0); iRow++)
    {
        for (int iCol = 1; iCol <= arrValue.GetLength(1); iCol++)
        {
             if (!string.IsNullOrEmpty(formulasWorksheet.Cells[iRow, iCol].Text))
             {
                  range.Cells[iRow, iCol].Formula = "=" + formulasWorksheet.Cells[iRow, iCol].Text; // restoring the formula
                  range.Cells[iRow, iCol].Value = range.Cells[iRow, iCol].Value // replacing the formula with the value
             }
        }

    }
}
于 2011-09-07T05:38:02.443 回答
0

在您的评论中,您陈述了您真正关心的问题,即如果自动重新计算单元格公式,性能将会受到影响。然后解决方案是防止自动重新计算,即使其手动:

Application.Calculation = xlCalculationManual

其中xlCalculationManual是一个内置常数,等于 -4135。您可以稍后将其设置回xlCalculationAutomatic或 -4105。

只需将公式留在那里,不要编辑值,让 Excel 找出结果。

在手动模式下,用户可以使用 强制重新计算F9,或者您可以使用 以编程方式进行Application.Calculate

所有这些在 Excel 用户界面中也可用:工具 > 选项 > 计算。

于 2011-09-07T09:46:25.797 回答