【问题标题】:Using EPPlus Excel - How to ignore excel error checking or remove green tag on top left of the cell使用 EPPlus Excel - 如何忽略 Excel 错误检查或删除单元格左上角的绿色标记
【发布时间】:2012-08-05 04:26:04
【问题描述】:

我使用 EPPlus 导出 excel 2007 文件。该文件可以正常导出,但我在设置列格式时遇到了一些问题。我的数字样式的字符串列(采购订单号 ex. 49000001)在每个单元格的左上角用绿色标签导出,我该如何删除它?

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

请帮忙。

ps 我用的是 C#

【问题讨论】:

    标签: c# excel epplus


    【解决方案1】:

    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 解决方案,将其包含在您的项目中,您将能够使用类似于此的代码删除标签:

    //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;
    

    【讨论】:

    • 请注意最后几行应该是// Do not display the warning 'number stored as text' sheet.IgnoredError.NumberStoredAsText = True; 感谢您发布此信息!效果很好!
    • 请注意,在此解决方法中设置范围很重要。我对此不小心,打开工作簿时出现 Excel 错误。
    • @briddums 我无法编辑源代码,有没有办法“从外部”编辑 XML?
    • @briddums 如果我使用您的代码,它只会抑制错误还是会转换底层数据类型?因为当我们实施该解决方案时,客户端强制要求 Excel 数据可用,即可以用作常规 Excel 表来执行 AVG、SUM 等操作。提前致谢。
    【解决方案2】:

    除了@briddums 的回答,从 EPPlus 版本 5 开始,您可以忽略错误,无需接触 EPPlus 源代码。

    var p = new ExcelPackage();
    var ws = p.Workbook.Worksheets.Add("IgnoreErrors");
    
    ws.Cells["A1"].Value = "1";
    ws.Cells["A2"].Value = "2";
    var ie = ws.IgnoredErrors.Add(ws.Cells["A2"]);
    ie.NumberStoredAsText = true;   // Ignore errors on A2 only
    

    【讨论】:

      【解决方案3】:

      此代码有效

      private void removingGreenTagWarning(ExcelWorksheet template1, string address)
                  {
                      var xdoc = template1.WorksheetXml;
                      //Create the import nodes (note the plural vs singular
                      var ignoredErrors = xdoc.CreateNode(System.Xml.XmlNodeType.Element, "ignoredErrors", xdoc.DocumentElement.NamespaceURI);
                      var ignoredError = xdoc.CreateNode(System.Xml.XmlNodeType.Element, "ignoredError", xdoc.DocumentElement.NamespaceURI);
                      ignoredErrors.AppendChild(ignoredError);
      
                      //Attributes for the INNER node
                      var sqrefAtt = xdoc.CreateAttribute("sqref");
                      sqrefAtt.Value = address;// 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);
                  }
      

      把它当作

      removingGreenTagWarning(template1, template1.Cells[1, 1, 100, 100].Address);
      

      【讨论】:

        【解决方案4】:

        将 Purchase Order No 值转换为 Number,然后存储在单元格中。每个单元格左上角的绿色标签即将出现,因为您将数字作为字符串存储在那里。

        【讨论】:

          【解决方案5】:

          是的,EPPlus 不能根据其值将数据视为数字。如果您的基础数据是字符串数据类型,那么您可以尝试以数字数据类型获取它。 例如如果您从存储过程中获取数据,请尝试将其作为数值获取。我们在实现它时遇到了一个问题。我们对 Web 使用相同的存储过程并生成 excel。出于格式化原因,Web UI 需要它是字符串数据类型,而 EPPlus 显然需要它是数字格式,以便可以将其显示为数字。解决方案是在 C# 代码中使用 EPPlus 导出到 excel 时将所需数据转换为数字。因此,您需要编写一个转换函数,将 DataTable 中的必填字段转换为您需要的数据类型(或在存储过程中使用 cast 或 convert 实现转换逻辑)。

          总而言之: - 编写一个 C# 函数来转换您获得的 DataTable 中列的数据类型,然后使用 EPPlus 将其作为 excel 表发送。

          【讨论】:

            【解决方案6】:

            我以更简单的方式解决了这个问题。 例如。如果我将值“123”定义为对象,而不是字符串,那么它将存储到 excel 文件中。

            【讨论】:

            • 不幸的是,将值 000123 存储为对象而不是字符串将使其在 excel 文件中显示为 123 而不是 000123。字符串对于保持文本格式很重要。
            【解决方案7】:

            你可以使用

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-02-17
              • 1970-01-01
              • 2018-11-20
              相关资源
              最近更新 更多