/// <summary> /// 删除sheet /// </summary> /// <param name="fileName"></param> /// <param name="sheetToDelete"></param> /// <returns></returns> public static bool XLDeleteSheet(string fileName, string sheetToDelete) { bool returnValue = false; using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(fileName, true)) { XmlDocument doc = new XmlDocument(); doc.Load(xlDoc.WorkbookPart.GetStream()); XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable); nsManager.AddNamespace("d", doc.DocumentElement.NamespaceURI); string searchString = string.Format("//d:sheet[@name='{0}']", sheetToDelete); XmlNode node = doc.SelectSingleNode(searchString, nsManager); if (node != null) { XmlAttribute relationAttribute = node.Attributes["r:id"]; if (relationAttribute != null) { string relId = relationAttribute.Value; xlDoc.WorkbookPart.DeletePart(relId); node.ParentNode.RemoveChild(node); doc.Save(xlDoc.WorkbookPart.GetStream(FileMode.Create)); returnValue = true; } } } return returnValue; }
分享和发布原创的新技术,中国的软件行业落后外国几百条街了,知识共享力争不要落后太多