Beispiel #1
0
        /// <summary>
        /// Sets a password for the sheet.
        /// </summary>
        /// <param name="Password"></param>
        public void SetPassword(string Password)
        {
            if (IsProtected == false)
            {
                IsProtected = true;
            }

            Password = Password.Trim();
            if (Password == "")
            {
                string  passwordPath = "d:sheetProtection";
                XmlNode node         = TopNode.SelectSingleNode(passwordPath, NameSpaceManager);
                if (node != null)
                {
                    TopNode.RemoveChild(node);
                    //node.OwnerDocument.Attributes.Remove(node as XmlAttribute);
                    //(node as XmlAttribute).OwnerElement.Attributes.Remove(node as XmlAttribute);
                }
                return;
            }

            int hash = EncryptedPackageHandler.CalculatePasswordHash(Password);

            SetXmlNodeString(_passwordPath, ((int)hash).ToString("x"));
        }
        internal bool Remove(ExcelThreadedComment comment)
        {
            var index = _commentList.IndexOf(comment);

            _commentList.Remove(comment);
            var commentNode = TopNode.SelectSingleNode("tc:threadedComment[@id='" + comment.Id + "']", NameSpaceManager);

            if (commentNode != null)
            {
                TopNode.RemoveChild(commentNode);

                //Reset the parentid to the first item in the list if we remove the first comment
                if (index == 0 && _commentList.Count > 0)
                {
                    ((XmlElement)_commentList[0].TopNode).RemoveAttribute("parentId");
                    for (int i = 1; i < _commentList.Count; i++)
                    {
                        _commentList[i].ParentId = _commentList[0].Id;
                    }
                }

                RebuildIndex();

                return(true);
            }

            return(false);
        }
 /// <summary>
 /// Removes all <see cref="ExcelThreadedComment"/>s in the collection
 /// </summary>
 internal void Clear()
 {
     foreach (var node in _commentList.Select(x => x.TopNode))
     {
         TopNode.RemoveChild(node);
     }
     _commentList.Clear();
 }
 /// <summary>
 /// Removes an item
 /// </summary>
 /// <param name="Item"></param>
 public void Remove(ExcelRichText Item)
 {
     TopNode.RemoveChild(Item.TopNode);
     _list.Remove(Item);
     if (_cells != null && _list.Count == 0)
     {
         _cells.IsRichText = false;
     }
 }
 /// <summary>
 /// Removes an item at the specific index
 /// </summary>
 /// <param name="Index"></param>
 public void RemoveAt(int Index)
 {
     TopNode.RemoveChild(_list[Index].TopNode);
     _list.RemoveAt(Index);
     if (_cells != null && _list.Count == 0)
     {
         _cells.IsRichText = false;
     }
 }
Beispiel #6
0
        /// <summary>
        /// Completely remove the xml node, resetting the properties to it's default values.
        /// </summary>
        public void Delete()
        {
            var node = GetNode(_path);

            if (node != null)
            {
                TopNode.RemoveChild(node);
            }
        }
Beispiel #7
0
 private void CreateFillTopNode(eFillStyle value)
 {
     if (_fillTypeNode != null)
     {
         TopNode.RemoveChild(_fillTypeNode);
     }
     CreateNode(_fillPath + "/a:" + GetStyleText(value), false);
     _fillNode = TopNode.SelectSingleNode(_fillPath + "/a:" + GetStyleText(value), NameSpaceManager);
 }
Beispiel #8
0
		private void UpdateDefinedNamesXml()
		{
			try
			{
                XmlNode top = WorkbookXml.SelectSingleNode("//d:definedNames", NameSpaceManager);
				if (!ExistsNames())
				{
					if (top != null) TopNode.RemoveChild(top);
					return;
				}
				else
				{
					if (top == null)
					{
						CreateNode("d:definedNames");
						top = WorkbookXml.SelectSingleNode("//d:definedNames", NameSpaceManager);
					}
					else
					{
						top.RemoveAll();
					}
					foreach (ExcelNamedRange name in _names)
					{

						XmlElement elem = WorkbookXml.CreateElement("definedName", ExcelPackage.schemaMain);
						top.AppendChild(elem);
						elem.SetAttribute("name", name.Name);
						if (name.IsNameHidden) elem.SetAttribute("hidden", "1");
						if (!string.IsNullOrEmpty(name.NameComment)) elem.SetAttribute("comment", name.NameComment);
						SetNameElement(name, elem);
					}
				}
				foreach (ExcelWorksheet ws in _worksheets)
				{
                    if (!(ws is ExcelChartsheet))
                    {
                        foreach (ExcelNamedRange name in ws.Names)
                        {
                            XmlElement elem = WorkbookXml.CreateElement("definedName", ExcelPackage.schemaMain);
                            top.AppendChild(elem);
                            elem.SetAttribute("name", name.Name);
                            elem.SetAttribute("localSheetId", name.LocalSheetId.ToString());
                            if (name.IsNameHidden) elem.SetAttribute("hidden", "1");
                            if (!string.IsNullOrEmpty(name.NameComment)) elem.SetAttribute("comment", name.NameComment);
                            SetNameElement(name, elem);
                        }
                    }
				}
			}
			catch (Exception ex)
			{
				throw new Exception("Internal error updating named ranges ",ex);
			}
		}
        /// <summary>
        /// Removes the validations that matches the predicate
        /// </summary>
        /// <param name="match">Predicate to filter out matching validations.</param>
        public void RemoveAll(Predicate <IExcelDataValidation> match)
        {
            var matches = _validations.FindAll(match);

            foreach (var m in matches)
            {
                if (!(m is ExcelDataValidation))
                {
                    throw new InvalidCastException("The supplied item must inherit OfficeOpenXml.DataValidation.ExcelDataValidation");
                }
                TopNode.RemoveChild(((ExcelDataValidation)m).TopNode);
            }
            _validations.RemoveAll(match);
            this.OnValidationCountChanged();
        }
        /// <summary>
        /// Removes an <see cref="ExcelDataValidation"/> from the collection.
        /// </summary>
        /// <param name="item">The item to remove</param>
        /// <returns>True if remove succeeds, otherwise false</returns>
        /// <exception cref="ArgumentNullException">if <paramref name="item"/> is null</exception>
        public bool Remove(IExcelDataValidation item)
        {
            if (!(item is ExcelDataValidation))
            {
                throw new InvalidCastException("The supplied item must inherit OfficeOpenXml.DataValidation.ExcelDataValidation");
            }
            Require.Argument(item).IsNotNull("item");
            TopNode.RemoveChild(((ExcelDataValidation)item).TopNode);
            var retVal = _validations.Remove(item);

            if (retVal)
            {
                OnValidationCountChanged();
            }
            return(retVal);
        }
Beispiel #11
0
        internal void RenameNode(XmlNode node, string prefix, string newName, string[] allowedChildren = null)
        {
            var doc     = node.OwnerDocument;
            var newNode = doc.CreateElement(prefix, newName, NameSpaceManager.LookupNamespace(prefix));

            while (TopNode.ChildNodes.Count > 0)
            {
                if (allowedChildren == null || allowedChildren.Contains(TopNode.ChildNodes[0].LocalName))
                {
                    newNode.AppendChild(TopNode.ChildNodes[0]);
                }
                else
                {
                    TopNode.RemoveChild(TopNode.ChildNodes[0]);
                }
            }
            TopNode.ParentNode.ReplaceChild(newNode, TopNode);
            TopNode = newNode;
        }
        /// <summary>
        /// Removes the validations that matches the predicate
        /// </summary>
        /// <param name="match"></param>
        public void RemoveAll(Predicate <IExcelDataValidation> match)
        {
            var matches = _validations.FindAll(match);

            foreach (var m in matches)
            {
                if (!(m is ExcelDataValidation))
                {
                    throw new InvalidCastException("The supplied item must inherit OfficeOpenXml.DataValidation.ExcelDataValidation");
                }
                TopNode.RemoveChild(((ExcelDataValidation)m).TopNode);
                //var dvNode = TopNode.SelectSingleNode(DataValidationPath.TrimStart('/'), NameSpaceManager);
                //if (dvNode != null)
                //{
                //    dvNode.RemoveChild(((ExcelDataValidation)m).TopNode);
                //}
            }
            _validations.RemoveAll(match);
            OnValidationCountChanged();
        }
Beispiel #13
0
 internal void SetXmlNodeBool(string path, bool value, bool removeIf)
 {
     if (value == removeIf)
     {
         var node = TopNode.SelectSingleNode(path, NameSpaceManager);
         if (node != null)
         {
             if (node is XmlAttribute)
             {
                 var elem = (node as XmlAttribute).OwnerElement;
                 elem.ParentNode.RemoveChild(elem);
             }
             else
             {
                 TopNode.RemoveChild(node);
             }
         }
     }
     else
     {
         SetXmlNodeString(TopNode, path, value ? "1" : "0", false, false);
     }
 }
Beispiel #14
0
 /// <summary>
 /// Remove the specified item
 /// </summary>
 /// <param name="Item">The item</param>
 public void Remove(ExcelRichText Item)
 {
     TopNode.RemoveChild(Item.TopNode);
 }