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