static void DeletePart <T>(WorksheetPart wp) where T : OpenXmlPart { var parts = wp.GetPartsOfType <T>().ToList(); if (parts.Any()) { foreach (var part in parts) { wp.DeletePart(part); } } }