简体   繁体   English

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

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

I've got this formula that generates the correct data: 我有这个公式可以生成正确的数据:

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

The problem is that it overthinks/micro-manages matters, and wonders if an adjacent cell (the one to the left, I presume, "Total Orders") should be included in the formula. 问题在于它过分考虑/微观管理,并且想知道是否应在公式中包括一个相邻的单元格(我想是左侧的单元格,“总订单数”)。

Specifically, if I click on the green triangle, a "!" 具体来说,如果我单击绿色三角形,则“!” glyph pops out; 字形弹出; clicking that has a msg, " Formula Omits Adjacent Cells " 单击带有msg的“ 公式忽略相邻单元格

And so, it tags all the formula-bound cells with a green triangle. 因此,它将所有公式绑定的单元格标记为绿色三角形。

Here is how it looks: 外观如下:

在此处输入图片说明

As you can see, the column with the formula always sports the little green triangle in the NW corner. 如您所见,带有公式的列始终在NW角处带有绿色小三角形。 And the formula is correct (averaging the values from "Sun Orders" through "Sat Orders" inclusive). 并且公式是正确的(将“ Sun订单”到“ Sat订单”之间的值进行平均)。

What can I do to tell Excel to "cool it" and prevent the little green triangles from displaying? 我该怎么做才能告诉Excel“冷却”并阻止显示绿色小三角形?

UPDATE UPDATE

Both answers so far seem reasonable, but neither work. 到目前为止,这两个答案似乎都是合理的,但都没有用。

I must admit, though, that my main Excel object is a little different than what Varocarbas has. 但是,我必须承认,我的主要Excel对象与Varocarbas的对象有些不同。 My code is: 我的代码是:

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

IOW, I'm using ApplicationClass instead of Excel.Application. IOW,我使用的是ApplicationClass而不是Excel.Application。

UPDATE 2 更新2

I changed my code to this (no longer using ApplicationClass): 我将代码更改为此(不再使用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;

...but I still get the little green triangles. ...但是我仍然得到绿色的小三角形。 Worse yet, I am starting to hear Bobby Goldsboro singing "God didn't make little green triangles" in my mind's ear. 更糟糕的是,我开始听到鲍比·戈德斯伯勒(Bobby Goldsboro)在我的耳边唱着“上帝并没有创造出绿色的三角形”。

UPDATE 3 更新3

I reckon the problem is probably not "closing properly the Excel processes." 我认为问题可能不是“正确关闭Excel进程”。 In Task Manager, I see quite a few "EXCEL.EXE *32" instances. 在任务管理器中,我看到了很多“ EXCEL.EXE * 32”实例。 I must be failing to 86 the Excel processes; 我一定无法通过86个Excel进程。 here is my code that I thought should do that: 这是我认为应该执行的代码:

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)

UPDATE 4 更新4

I'm not at all sure this is the very best way to do it, but the order of releasing makes sense to me (sheet, then sheets, then book, then app), and empirically speaking, I am not only no longer seeing the little green meanies, but I have no more extraneous/superfluous instances of Excel lurking about the innards of the devbox. 我完全不确定这是最好的方法,但是发布的顺序对我来说很有意义(先上一页,再上一页,然后是书,再上应用程序),从经验上讲,我不仅不再看到小小的绿色小工具,但我再也没有多余的/多余的Excel实例潜伏在devbox的内部。

Here is what I changed the above release and null mess to: 这是我将上述版本和null混乱更改为的内容:

    . . .
    }
    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);

I may not need all of it (such as _xlBook.Close() and _xlApp.Quit), but at least it's working this way. 我可能不需要全部(例如_xlBook.Close()和_xlApp.Quit),但至少它是以这种方式工作的。

This green triangle indicates that the formula in the given cell contains an error according to certain rules ( more information about this ). 此绿色三角形表示,根据某些规则,给定单元格中的公式包含错误( 有关此信息,更多信息 )。

You can enable/disable this behaviour at the Excel application level, by changing the value of the property ErrorCheckingOptions.BackgroundChecking . 您可以通过更改属性ErrorCheckingOptions.BackgroundChecking的值在Excel应用程序级别启用/禁用此行为。 Sample code: 样例代码:

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

尝试添加以下行

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

My answer is pretty close to Ivan's response. 我的回答与Ivan的回答非常接近。 Other Error Types for cells can be found here on msdn . 对细胞的其他错误类型可以在这里找到在MSDN

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#Excel Interop中格式化公式化结果? - How can I format a formulaic result in C# Excel Interop? 如何使用 Excel Interop 检索与基于引用另一个 Excel 工作表的公式的 Excel 单元格关联的值? - How can I retrieve the value associated with an Excel Cell that is based on a formula referencing another Excel Sheet using Excel Interop? C#Excel Interop公式拖动 - C# excel interop formula dragging C# 防止用户在 excel 互操作中丢失数据 - C# Prevent users from losing data in excel interop 在C#中,如何使用Excel Interop加快写入多个单元格值的速度 - In C#, how do I use the Excel Interop to speed up writing several cell values 如何在C#Interop中保留Excel单元格格式? - How to persist Excel cell formats in C# Interop? 如何使用C#和Interop在Excel中设置锁定的单元格保护选项? - How do I set locked cell protection options in Excel with C# and Interop? 如何使用C#访问Excel工作表中的公式? - how can I access the formula in an excel sheet with c#? 如何用 c# 编写 excel 公式直到 excel 的最后一行(互操作)? - How to write excel formula with c# until last row of the excel (interop)? C# COM Interop Excel:如何使用 Interop Excel 从 C# 写入单元格? - C# COM Interop Excel: How to write to cells from C# using Interop Excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM