如何防止绿色警告三角形显示在公式绑定单元格中 (C# Excel Interop)?

How can I prevent the green warning triangle from displaying in a formula-bound cell (C# Excel Interop)?

我有生成正确数据的公式:

var avgWeeklyDeliveriesCell = (Range)_xlSheetDelPerf.Cells[curDelPerfRow, AVG_WEEKLY_DELIVERIES_COLUMN];
avgWeeklyDeliveriesCell.Value2 = string.Format("=ROUND(AVERAGE(C{0}:I{0}), 2)", curDelPerfRow);

问题是它 overthinks/micro-manages 很重要,想知道是否应该在公式中包含相邻的单元格(左边的单元格,我想是 "Total Orders")。

具体来说,如果我点击绿色三角形,一个“!”字形弹出;单击有消息,“公式省略相邻单元格

因此,它用绿色三角形标记所有公式绑定的单元格。

外观如下:

如您所见,包含公式的列始终在 NW 角显示绿色小三角形。并且公式是正确的(从 "Sun Orders" 到 "Sat Orders" 取平均值)。

我该怎么做才能告诉 Excel 到 "cool it" 并阻止显示绿色小三角形?

更新

到目前为止,这两个答案似乎都合理,但都不起作用。

不过,我必须承认,我的主要 Excel 对象与 Varocarbas 的对象略有不同。我的代码是:

using Microsoft.Office.Interop.Excel;
using Application = System.Windows.Forms.Application;
. . .
private ApplicationClass _xlApp;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ErrorCheckingOptions.BackgroundChecking = false;

IOW,我正在使用 ApplicationClass 而不是 Excel.Application。

更新 2

我将我的代码更改为(不再使用 ApplicationClass):

using Excel = Microsoft.Office.Interop.Excel;
. . .
//private Excel.ApplicationClass _xlApp;
private Excel.Application _xlApp = new Excel.Application();
. . .

//_xlApp = new Excel.ApplicationClass { UserControl = true };
_xlApp = new Excel.Application();
_xlApp.ErrorCheckingOptions.BackgroundChecking = false;

...但我仍然看到绿色的小三角形。更糟糕的是,我开始在脑海中听到 Bobby Goldsboro 的歌声 "God didn't make little green triangles"。

更新 3

我估计问题可能不是 "closing properly the Excel processes." 在任务管理器中,我看到不少 "EXCEL.EXE *32" 实例。我一定是 86 Excel 进程失败了;这是我认为应该这样做的代码:

foreach (DataRowView drv in selectedUnits)
{
    Application.DoEvents();
    _xlApp = new Excel.Application();
    _xlApp.ErrorCheckingOptions.BackgroundChecking = false;

    _xlBook = _xlApp.Workbooks.Add(Type.Missing);
    _xlBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    _xlApp.ActiveWindow.DisplayGridlines = false;
    _xlApp.SheetsInNewWorkbook = 1; // prevent the empty "sheet 2" etc.
    _xlSheets = _xlBook.Worksheets;

    _xlSheet = (Excel.Worksheet)_xlSheets.Item[1];
    if (_xlSheet != null)
    {
        _xlSheet.Name = ProduceUsageByMonthSheetName;   
        . . .
        var filename = Properties.Settings.Default.OutputDirectory + shortUnitName + ".xlsx";
        if (File.Exists(filename))
        {
            File.Delete(filename);
        }

        _xlBook.SaveAs(Properties.Settings.Default.OutputDirectory + shortUnitName + ".xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        Marshal.ReleaseComObject(_xlSheet);
        _xlBook.Close(false, null, null);
        Marshal.ReleaseComObject(_xlBook);
        _xlApp.DisplayAlerts = false;
        _xlApp.Quit();
        _xlSheets = null;
    }
    _xlSheet = null;
    _xlBook = null;
    _xlApp = null;
    OnChanged(EventArgs.Empty);
} // foreach (DataRowView drv in selectedUnits)

更新 4

我不确定这是最好的方法,但发布的顺序对我来说很有意义(sheet,然后是 sheets,然后是书,然后应用程序),根据经验,我不仅不再看到小绿人,而且我也没有更多 extraneous/superfluous 个 Excel 实例潜伏在 devbox 的内部。

下面是我把上面的release和null mess改成:

    . . .
    }
    Marshal.ReleaseComObject(_xlSheet);

    Marshal.ReleaseComObject(_xlSheets);

    _xlBook.Close(false, null, null);
    Marshal.ReleaseComObject(_xlBook);

    _xlApp.DisplayAlerts = false;
    _xlApp.Quit();
} // foreach (DataRowView drv in selectedUnits)
Marshal.ReleaseComObject(_xlApp);

我可能不需要全部(例如 _xlBook.Close() 和 _xlApp.Quit),但至少它是这样工作的。

尝试添加以下行

avgWeeklyDeliveriesCell.Errors[XlErrorChecks.xlInconsistentFormula].Ignore = true;

这个绿色三角形表示给定单元格中的公式根据某些规则包含错误 (more information about this)。

您可以在 Excel 应用程序级别 enable/disable 此行为,方法是更改​​ 属性 ErrorCheckingOptions.BackgroundChecking 的值。示例代码:

Excel.Application excelApp = new Excel.Application();
excelApp.ErrorCheckingOptions.BackgroundChecking = false;

我的回答与 Ivan 的回答非常接近。可以在 msdn.

上找到单元格的其他错误类型
avgWeeklyDeliveriesCell.Errors[XlErrorChecks.xlOmittedCells].Ignore = true;