Manage Chart Sheets
- 5 minutes to read
The Spreadsheet Document API allows you to create, organize, rename, hide, copy, and remove chart sheets. A chart sheet displays a single chart in a separate workbook sheet.
Access Chart Sheets
Use the Workbook.ChartSheets property to access the workbook’s ChartSheetCollection. Use Workbook.Sheets to access both worksheets and chart sheets. Collection indexes are zero-based.
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook(File.ReadAllBytes(@"..\..\..\Data\Book.xlsx"));
ChartSheet chartsheet = workbook.ChartSheets[0];
// Do something with the chart sheet.
Create a Chart Sheet
Create a ChartSheet instance and assign a Chart to its constructor. Call Add to append the chart sheet to the Workbook.ChartSheets collection. The API also adds the chart sheet to the Workbook.Sheets collection.
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook(File.ReadAllBytes(@"..\..\..\Data\Book.xlsx"));
ChartSheet chartSheet = new ChartSheet("ChartSheet1", new Chart());
workbook.ChartSheets.Add(chartSheet);
If a chart sheet instance has already been added to the collection, further Add calls only move this object to the end of the collection and do not create a copy.
For more information about naming conventions for chart sheets, refer to the following section: Rename a Chart Sheet.
Insert a Chart Sheet
Call the Insert method to place a chart sheet at a specific zero-based index:
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook(File.ReadAllBytes(@"..\..\..\Data\Book.xlsx"));
ChartSheet chartSheet = new ChartSheet("ChartSheet1", new Chart());
workbook.ChartSheets.Insert(0, chartSheet);
The index specifies a position in the ChartSheets collection and does not count worksheets. In tab order (Workbook.Sheets), the API places the chart sheet before the chart sheet that occupied this position. For example, if a workbook contains the Sheet1 worksheet and the Chart1 chart sheet, a call to ChartSheets.Insert(0, chartSheet) places the new chart sheet between Sheet1 and Chart1.
If a chart sheet instance has already been added to the collection, further Insert calls only move this object to another position and do not create a copy.
Access and Replace a Chart
Use the ChartSheet.Chart property to access or replace the chart displayed on a chart sheet. You can assign a new Chart or ChartEx instance to this property.
using DevExpress.Docs.Spreadsheet;
// Create a workbook.
Workbook workbook = new Workbook(File.ReadAllBytes(@"..\..\..\Data\Book.xlsx"));
// Create and add a chart sheet.
ChartSheet chartSheet = new ChartSheet("ChartSheet1", new Chart());
workbook.ChartSheets.Add(chartSheet);
// Replace the chart.
chartSheet.Chart = new ChartEx();
Create a Chart Sheet Copy
Call ChartSheet.Clone to create a copy of a chart sheet. Assign the copy a unique name before you add it to a workbook.
using DevExpress.Docs.Spreadsheet;
// Create a workbook and chart sheet.
Workbook workbook = new Workbook();
ChartSheet chartSheet = new ChartSheet("Sales", new Chart());
workbook.ChartSheets.Add(chartSheet);
// Clone the chart sheet and assign a unique name.
ChartSheet copy = chartSheet.Clone();
copy.Name = "Sales Copy";
// Add the chart sheet copy.
workbook.ChartSheets.Add(copy);
Rename a Chart Sheet
Set the Sheet.Name property to rename a chart sheet.
Chart sheet names must follow the following rules:
- A chart sheet name must not be equal to a name that is already contained in the collection of existing sheets (Workbook.Sheets).
- A chart sheet name must not exceed 31 characters.
- A chart sheet name must not contain the following symbols: \, /, ?, :, *, [ or ].
- The chart sheet name must not start and end with a single quote (‘).
- A chart sheet name must not be an empty string.
Manage Chart Sheet Visibility
Set the Sheet.Visibility property to hide a chart sheet. Use SheetVisibility enumeration values to specify the visibility state. The following values are available:
| Value | Description |
|---|---|
Visible |
The chart sheet is visible. |
Hidden |
The chart sheet is hidden but can be made visible again in Excel. |
VeryHidden |
The chart sheet is hidden and cannot be made visible again in Excel. |
Remove a Chart Sheet
Call Remove to remove a chart sheet instance, or call RemoveAt to remove a chart sheet at a zero-based index.
A workbook must contain at least one visible sheet. Worksheets and chart sheets count together. If the workbook contains a single visible sheet, Remove and RemoveAt methods do not remove this sheet and throw an InvalidOperationException. The API throws the same exception if you assign a hidden sheet to the collection indexer at the position of this sheet.