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. It also converts Excel documents to PDF files. Using this library, you can show or hide Excel chart legends in C# and VB.NET. The HasLegend property of the IChart interface is used to show or hide the 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 a 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 legends in C# and VB.NET.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
// Instantiate the application object
IApplication application = excelEngine.Excel;
// Open the workbook containing the chart
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
// Access the first worksheet
IWorksheet worksheet = workbook.Worksheets[0];
// Access the first chart in the worksheet
IChart chart = worksheet.Charts[0];
// To hide the legend in the 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 the chart
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
'Access the first worksheet
Dim sheet As IWorksheet = workbook.Worksheets(0)
'Access the first chart in the worksheet
Dim chart As IChart = sheet.Charts(0)
'To hide the legend in the 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?
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to Show or hide Excel chart legend in C#, VB.NET.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!