Show or hide Excel chart legend in C#, VB.NET
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can show or hide Excel chart legend in C#, VB.NET. HasLegend property of IChart interface is used to show or hide Excel chart legend. By default, this property is set to true.
Steps to show or hide Excel chart legend, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package
Step 3: Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 4: Use the following code snippet to show or hide Excel chart legend in C#, VB.NET.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the application object IApplication application = excelEngine.Excel; //Open the workbook containing chart IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic); //Accessing the first worksheet IWorksheet worksheet = workbook.Worksheets[0]; //Accessig first chart in the worksheet IChart chart = worksheet.Charts[0]; //To hide legend in chart chart.HasLegend = false; //Save the file string fileName = "Output.xlsx"; workbook.SaveAs(fileName); System.Diagnostics.Process.Start(Path.GetFullPath(fileName)); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the application object Dim application As IApplication = excelEngine.Excel 'Open the workbook containing chart Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic) 'Accessing the first worksheet Dim sheet As IWorksheet = workbook.Worksheets(0) 'Accessig first chart in the worksheet Dim chart As IChart = sheet.Charts(0) 'To hide legend in chart chart.HasLegend = False 'Save the file Dim fileName As String = "Output.xlsx" workbook.SaveAs(fileName) System.Diagnostics.Process.Start(Path.GetFullPath(fileName)) End Using
A complete Windows Forms working example of how to copy Excel worksheet to another workbook in C# and VB can be downloaded from Show or hide Excel chart legend.zip.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
See Also:
How to make visible the grid lines of primary category axis in the chart?
How to add text box shape in Excel charts?
How to create Excel line with markers chart in C#, VB>NET?
How to customize the items in the ChartLegend?
How to change the color of series?
How to make the secondary axis visible in chart?
How to reverse Excel chart axis order in C#, VB.NET?
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.