From 8e2cac0b060a89279c65bec6f9f0d939812538f1 Mon Sep 17 00:00:00 2001 From: Lieven De Foor Date: Thu, 28 May 2026 14:19:03 +0200 Subject: [PATCH] Fix: ExcelWorksheets.Delete throws NotSupportedException on ExcelChartsheet The Delete method was attempting to access the PivotTables collection on all worksheet types. However, ExcelChartsheet throws NotSupportedException when accessing PivotTables. Added a type guard to skip PivotTable cleanup for chart sheets. --- src/EPPlus/ExcelWorksheets.cs | 17 +++++---- .../Issues/ChartSheetDeleteTests.cs | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 src/EPPlusTest/Issues/ChartSheetDeleteTests.cs diff --git a/src/EPPlus/ExcelWorksheets.cs b/src/EPPlus/ExcelWorksheets.cs index 789e50cf55..9878e1c686 100644 --- a/src/EPPlus/ExcelWorksheets.cs +++ b/src/EPPlus/ExcelWorksheets.cs @@ -470,15 +470,18 @@ public void Delete(int Index) worksheet.Drawings.ClearDrawings(); } - //Remove all comments - if (!(worksheet is ExcelChartsheet) && worksheet.Comments.Count > 0) + if (!(worksheet is ExcelChartsheet)) { - worksheet.Comments.Clear(); - } + //Remove all comments + if (worksheet.Comments.Count > 0) + { + worksheet.Comments.Clear(); + } - while(worksheet.PivotTables.Count>0) - { - worksheet.PivotTables.Delete(worksheet.PivotTables[0]); + while (worksheet.PivotTables.Count > 0) + { + worksheet.PivotTables.Delete(worksheet.PivotTables[0]); + } } //Delete any parts still with relations to the Worksheet. DeleteRelationsAndParts(worksheet.Part); diff --git a/src/EPPlusTest/Issues/ChartSheetDeleteTests.cs b/src/EPPlusTest/Issues/ChartSheetDeleteTests.cs new file mode 100644 index 0000000000..8707aa7b6c --- /dev/null +++ b/src/EPPlusTest/Issues/ChartSheetDeleteTests.cs @@ -0,0 +1,35 @@ +/******************************************************************************* + * Required Notice: Copyright (C) EPPlus Software AB. + * https://epplussoftware.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + *******************************************************************************/ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OfficeOpenXml; +using OfficeOpenXml.Drawing.Chart; + +namespace EPPlusTest.Issues +{ + [TestClass] + public class ChartSheetDeleteTests + { + [TestMethod] + public void DeleteChartSheetShouldNotThrowNotSupportedException() + { + using (var package = new ExcelPackage()) + { + var ws = package.Workbook.Worksheets.Add("Data"); + var chartSheet = package.Workbook.Worksheets.AddChart("ChartSheet", eChartType.ColumnClustered); + + // This call previously threw NotSupportedException because it tried to access chartSheet.PivotTables + package.Workbook.Worksheets.Delete(chartSheet); + + Assert.AreEqual(1, package.Workbook.Worksheets.Count); + } + } + } +}