How can I turn background error checking off for the Excel app object in EPPlus?

女生的网名这么多〃 提交于 2021-01-29 04:45:19

问题


Using the unwieldy and ponderous but full-featured Excel Interop, background error checking can be toggled off like so:

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

...as shown here

I am getting the green triangles indicating a bad number like so:

...which I want to turn off. These are just string vals that should not be flagged as bad or suspicious.

So how can I turn background error checking off for the Excel app object, or otherwise programmatically prevent these green triangles, using EPPlus?

UPDATE

Changing the code from this:

using (var custNumCell = priceComplianceWorksheet.Cells[rowToPopulate, DETAIL_CUSTNUM_COL])
{
    custNumCell.Style.Font.Size = DATA_FONT_SIZE;
    custNumCell.Value = _custNumber;
    custNumCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}

...to this:

using (var custNumCell = priceComplianceWorksheet.Cells[rowToPopulate, DETAIL_CUSTNUM_COL])
{
    custNumCell.Style.Font.Size = DATA_FONT_SIZE;
    custNumCell.ConvertValueToAppropriateTypeAndAssign(_custNumber);
    custNumCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}

// Adapted from https://stackoverflow.com/questions/26483496/is-it-possible-to-ignore-excel-warnings-when-generating-spreadsheets-using-epplu
public static void ConvertValueToAppropriateTypeAndAssign(this ExcelRangeBase range, object value)
{
    string strVal = value.ToString();
    if (!String.IsNullOrEmpty(strVal))
    {
        decimal decVal;
        double dVal;
        int iVal;

        if (decimal.TryParse(strVal, out decVal))
        {
            range.Value = decVal;
        }
        else if (double.TryParse(strVal, out dVal))
        {
            range.Value = dVal;
        }
        else if (Int32.TryParse(strVal, out iVal))
        {
            range.Value = iVal;
        }
        else
        {
            range.Value = strVal;
        }
    }
    else
    {
        range.Value = null;
    }
}

...semi-fixed it; it is now:

But notice that the leading "0" got stripped out. I need that to remain, so this is still only half-solved.

UPDATE 2

I tried the suggestion from the comment below that pointed here, and added this code:

    //Create the import nodes (note the plural vs singular
    var ignoredErrors = 
priceComplianceWorksheet.CreateNode(XmlNodeType.Element, "ignoredErrors",
xdoc.DocumentElement.NamespaceURI);
    var ignoredError
priceComplianceWorksheet.CreateNode(XmlNodeType.Element, "ignoredError",
xdoc.DocumentElement.NamespaceURI);
    ignoredErrors.AppendChild(ignoredError);

    //Attributes for the INNER node
    var sqrefAtt = priceComplianceWorksheet.CreateAttribute("sqref");
    sqrefAtt.Value = range;

    var flagAtt =
priceComplianceWorksheet.CreateAttribute("numberStoredAsText");
    flagAtt.Value = "1";

    ignoredError.Attributes.Append(sqrefAtt);
    ignoredError.Attributes.Append(flagAtt);

    //Now put the OUTER node into the worksheet xml
   priceComplianceWorksheet.LastChild.AppendChild(ignoredErrors);

...but "CreateAttribute" and "LastChild" are not recognized...?!?


回答1:


In response to update 2, you just need to reference the XmlDocument and use that to generate the XML:

var xdoc = priceComplianceWorksheet.WorksheetXml;

//Create the import nodes (note the plural vs singular
var ignoredErrors = xdoc.CreateNode(XmlNodeType.Element, "ignoredErrors",xdoc.DocumentElement.NamespaceURI);
var ignoredError = xdoc.CreateNode(XmlNodeType.Element, "ignoredError",xdoc.DocumentElement.NamespaceURI);
ignoredErrors.AppendChild(ignoredError);

//Attributes for the INNER node
var sqrefAtt = xdoc.CreateAttribute("sqref");
sqrefAtt.Value = "C2:C10"; // Or whatever range is needed....

var flagAtt = xdoc.CreateAttribute("numberStoredAsText");
flagAtt.Value = "1";

ignoredError.Attributes.Append(sqrefAtt);
ignoredError.Attributes.Append(flagAtt);

//Now put the OUTER node into the worksheet xml
xdoc.LastChild.AppendChild(ignoredErrors);


来源:https://stackoverflow.com/questions/41334961/how-can-i-turn-background-error-checking-off-for-the-excel-app-object-in-epplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!