使用EPPlus Excel – 如何忽略Excel错误检查或删除单元格左上方的绿色标记

我使用EPPlus导出excel 2007文件。 该文件可以正常导出,但我有一些设置列格式的问题。 我的string列与数字样式(采购订单编号ex。49000001)导出与每个单元格左上angular的绿色标记,我如何删除它?

我尝试将数字格式设置为“常规”,但不起作用

请帮忙。

PS我使用C#

EPPLus目前不支持禁用该绿色标签。 但是,可以修改该项目以抑制该项目。 首先,您需要向项目添加一个新的类ExcelIgnoredError.cs:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace OfficeOpenXml { public class ExcelIgnoredError : XmlHelper { private ExcelWorksheet _worksheet; /// <summary> /// Constructor /// </summary> internal ExcelIgnoredError(XmlNamespaceManager ns, XmlNode node, ExcelWorksheet xlWorkSheet) : base(ns, node) { _worksheet = xlWorkSheet; } public bool NumberStoredAsText { get { return GetXmlNodeBool("@numberStoredAsText"); } set { SetXmlNodeBool("@numberStoredAsText", value); } } public bool TwoDigitTextYear { get { return GetXmlNodeBool("@twoDigitTextYear"); } set { SetXmlNodeBool("@twoDigitTextYear", value); } } public string Range { get { return GetXmlNodeString("@sqref"); } set { SetXmlNodeString("@sqref", value); } } } } 

接下来,您将需要修改ExcelWorkSheet.cs,添加以下代码:

 public ExcelIgnoredError _ignoredError; public ExcelIgnoredError IgnoredError { get { if (_ignoredError == null) { // Check that ignoredErrors exists XmlNode node = TopNode.SelectSingleNode("d:ignoredErrors", NameSpaceManager); if (node == null) { CreateNode("d:ignoredErrors"); } //Check that ignoredError exists node = TopNode.SelectSingleNode("d:ignoredErrors/d:ignoredError", NameSpaceManager); if (node == null) { CreateNode("d:ignoredErrors/d:ignoredError"); node = TopNode.SelectSingleNode("d:ignoredErrors/d:ignoredError", NameSpaceManager); } _ignoredError = new ExcelIgnoredError(NameSpaceManager, node, this); } return (_ignoredError); } } 

编译EPPPlus解决scheme,将其包含在您的项目中,您将能够使用类似于以下代码移除标签:

 //Get a reference to the worksheet ExcelWorkSheet sheet = package.WorkBook.WorkSheets(0); //Set the cell range to ignore errors on to the whole sheet sheet.IgnoredError.Range = Sheet.Dimension.Address; //Do not display the warning 'number stored as text' sheet.IgnoredError.NumberStoredAsText = true; 

将采购订单没有值转换为数字,然后存储在单元格中。 每个单元格左上angular的绿色标记即将到来,因为您将数字存储为string。

是的,EPPlus不能将数据视为数字,具体取决于其数值。 如果您的底层数据是string数据types,那么您可以尝试使用数字数据types。 例如,如果您从存储过程获取数据,请尝试将其作为数字值。 我们实施这个时遇到了一个问题。 我们使用相同的Web存储过程,并生成Excel。 为了格式化的原因,Web UI需要它是string数据types,而且EPPlus显然需要它是数字格式,以便它可以显示为数字。 解决scheme是在使用C#代码中的EPPlus导出为Excel时,将所需的数据转换为数字。 所以你需要一个转换函数来将DataTable中的必填字段转换为你所需要的数据types。

总结: – 编写一个C#函数,在使用EPPlus将其作为excel发送之前,将其获得的数据表中的数据types转换为数据types。

我已经以更简单的方式解决了这个问题。 例如,如果我定义值“123”作为对象,而不是一个string,那么它存储到Excel文件OK。

您可以使用

 worksheet.Cell[1,1].Formula = "TEXT(" + cellvalue ")";