Delete Legend and Specific Legend Entries from Excel Chart in C#

A legend is displayed in the chart area by default. However it can be removed from the chart. With Spire.XLS, we can delete the whole legend as well as specific legend entries from Excel chart. This article is going to demonstrate how we can use Spire.XLS to accomplish this function.

Below screenshot shows the Excel chart we used for demonstration:

Delete Legend and Specific Legend Entries from Excel Chart in C#

Delete the whole legend

using Spire.Xls;
namespace DeleteLegend
{

    class Program
    {

        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile("sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the chart
            Chart chart = sheet.Charts[0];

            //Delete legend from the chart
            chart.Legend.Delete();

            //Save the file                        
            workbook.SaveToFile("DeleteLegend.xlsx", ExcelVersion.Version2013);
        }
    }
}

Screenshot:

Delete Legend and Specific Legend Entries from Excel Chart in C#

Delete specific legend entries

using Spire.Xls;
namespace DeleteLegend
{

    class Program
    {

        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile("sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the chart
            Chart chart = sheet.Charts[0];

            //Delete the first and the second legend entries from the chart
            chart.Legend.LegendEntries[0].Delete();
            chart.Legend.LegendEntries[1].Delete();

            //Save the file                        
            workbook.SaveToFile("DeleteLegendEntries.xlsx", ExcelVersion.Version2013);

        }
    }
}

Screenshot:

Delete Legend and Specific Legend Entries from Excel Chart in C#