Get Worksheet of the Chart

Example

The following example shows how to use the Chart.getWorksheet() property. The code first prints the name of the worksheet, then accesses the first chart on the worksheet. It then prints the worksheet name again, using the Chart.getWorksheet() property.

Java code to access the worksheet of the chart

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(GetWorksheetOfChart.class);
// Create workbook from sample Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Access first worksheet of the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Print worksheet name
System.out.println("Sheet Name: " + worksheet.getName());
// Access the first chart inside this worksheet
Chart chart = worksheet.getCharts().get(0);
// Access the chart's sheet and display its name again
System.out.println("Chart's Sheet Name: " + chart.getWorksheet().getName());

Console output generated by the sample code

Below is the console output the sample code results in. As you can see, it prints the same worksheet name both times.

Sheet Name: Portfolio

Chart's Sheet Name: Portfolio