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

結果が定義されていないため、つまり式の結果が値はセルに何を表示しますか?

将来の再利用のために数式を保存したいので、数式を適用し、それらを辞書に保存する 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 に答える