How to retrieve the name of the chart in an Excel worksheet?

24 Jun 20263 minutes to read

The following code examples demonstrate retrieving the name of the chart in an Excel worksheet using C# (Cross-platform and Windows-specific) and VB.NET.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx"));
    IWorksheet worksheet = workbook.Worksheets[0];

    //Get the chart name 
    string chartName = worksheet.Charts[0].Name;
    //Display the chart name 
    Console.WriteLine("The name of the chart is: " + chartName);

    //Saving the workbook
    workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Open("Input.xlsx");
    IWorksheet worksheet = workbook.Worksheets[0];

    //Get the chart name 
    string chartName = worksheet.Charts[0].Name;
    //Display the chart name 
    Console.WriteLine("The name of the chart is: " + chartName);

    //Saving the workbook
    workbook.SaveAs("Output.xlsx");
}
Using excelEngine As New ExcelEngine()
    ' Access the IApplication instance
    Dim application As IApplication = excelEngine.Excel

    ' Set the default version
    application.DefaultVersion = ExcelVersion.Xlsx

    ' Open the input workbook
    Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")

    ' Get the first worksheet
    Dim worksheet As IWorksheet = workbook.Worksheets(0)

    ' Get the chart name
    Dim chartName As String = worksheet.Charts(0).Name

    ' Display the chart name
    Console.WriteLine("The name of the chart is: " & chartName)

    ' Save the workbook to output
    workbook.SaveAs("Output.xlsx")
End Using

A complete working example in C# is present on this GitHub page.