使用笨重而笨重但功能齐全的 Excel 互操作,可以像这样关闭后台错误检查:
Excel.Application excelApp = new Excel.Application();
excelApp.ErrorCheckingOptions.BackgroundChecking = false;
...如图所示
我得到的绿色三角形表示错误的数字,如下所示:
...我想关掉。这些只是不应标记为坏或可疑的字符串值。
那么如何使用 EPPlus 关闭 Excel 应用程序对象的背景错误检查,或者以其他方式以编程方式阻止这些绿色三角形?
更新
从此更改代码:
using (var custNumCell = priceComplianceWorksheet.Cells[rowToPopulate, DETAIL_CUSTNUM_COL])
{
custNumCell.Style.Font.Size = DATA_FONT_SIZE;
custNumCell.Value = _custNumber;
custNumCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
...对此:
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;
}
}
...半固定的;就是现在:
但请注意,前导的“0”被去掉了。我需要保留它,所以这仍然只解决了一半。
更新 2
我尝试了以下评论中指向此处的建议,并添加了以下代码:
//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);
...但是“CreateAttribute”和“LastChild”无法识别...?!?