如何closuresEPPlus中Excel应用程序对象的背景错误检查?

使用笨重笨重但function齐全的Excel Interop,后台错误检查可以如下切换:

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

…如图所示

我得到的绿色三angular形表示这样的一个坏数字:

在这里输入图像说明

…我想要closures。 这些只是stringvals不应被标记为坏或可疑。

那么,如何使用EPPlus将Excel应用程序对象的背景错误检查closures,或以其他方式编程阻止这些绿色三angular?

UPDATE

从这个改变代码:

 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

我尝试了下面这个评论的build议,并添加了这个代码:

  //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”不被识别…?!?

为了响应更新2,您只需引用XmlDocument并使用它来生成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);