Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

c# - Using EPPlus Excel - How to ignore excel error checking or remove green tag on top left of the cell

I use EPPlus to export excel 2007 file. The file can export normally but i have some problem with setting column format. My string column with numeric style (Purchase Order No. ex. 49000001) be exported with green tag on the top left of the each cell, How can i remove it?

I try to set number format to "General" but it's not work

Please help.

p.s i use C#

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EPPLus does not currently support disabling that green tag. However, it is possible to modify the project in order to suppress it. First you will need to add a new class to the project, 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);
            }
        }
    }
}


Next you will need to modify ExcelWorkSheet.cs, adding this code:

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);
    }
}


Compile the EPPPlus solution, include it in your project and you will be able to remove the tags using code similar to this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...