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

1

2

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:

enter image description here

...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://mcmap.net/q/911561/-is-it-possible-to-ignore-excel-warnings-when-generating-spreadsheets-using-epplus
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:

enter image description here

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...?!?

Subbase answered 26/12, 2016 at 18:27 Comment(2)
What about adding the option to ignore errors via xml: #32588334Fabled
That looked promising, but CreateAttribute and LastChild are not recognized. I'll add an Update with a little more detail.Subbase
F
4

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);
Fabled answered 27/12, 2016 at 17:48 Comment(1)
If you have more than one range to ignore create multiples of ignoredError and also their Attributes and append them to ignoredErrors. Ranges lie A:A,X:X don't work.Complicacy

© 2022 - 2024 — McMap. All rights reserved.