Add, Delete and Manipulate Charts in Excel file using C#

Charts are used to reflect information in easy to understand manner. Spreadsheets are used by almost all offices to manage data but they are not so easy to read. This is why a Chart, often known as a graph in Excel, is helpful to present an overview of the data. Aspose.Cells for Cloud API is rich with Chart manipulation APIs. In this article, we will be learning the following features:

  • Add Chart in Excel Spreadsheet
  • Delete Chart in Excel Spreadsheet
  • Update Existing Chart in Excel Spreadsheet

The API can be called from any platform using any programming language. Different SDKs are available including Python, PHP, Ruby, Perl, Go, Swift, and many more. You can choose and use it among any of those as per your convenience. Here we will be using C# code for these examples.

Add Chart in Excel Spreadsheet

You can insert a chart or graph in an Excel workbook with Aspose.Cells Cloud API. Simply specify the necessary properties of the chart, like Chart Type, Chart Area, and then make the API call. Below is a sample code which adds a Pie Chart to a worksheet:

This code snippet will create a chart as displayed in the following screenshot:

Add Chart in Excel

Delete Chart from Excel Spreadsheet

You can delete or remove an existing chart from an XLSX or XLS file with Aspose.Cells Cloud API. Simply specify the file name, worksheet name, and chart index. Subsequently, call the API and respective chart will be deleted from the worksheet. However, the data will remain unaffected in the cells as it is, only the chart will be deleted. Following code snippet can be used for the same purpose:

The below screenshot shows output worksheet where data stays the same and the chart is deleted.

Update a Chart in Excel Spreadsheet

Since we have already considered two of the chart manipulation features above, let us check out another feature of updating an existing chart. Here we will be updating the title of the chart by specifying the name of Excel file, name of the worksheet, and index of the chart as in following C# code snippet:

The output of this code snippet can be observed in the screenshot below. The chart title is the same as specified by us.

These are a few of the many chart manipulation features offered by the API. You can sign up for free and test the APIs in their full capacity. In case of any query, please write to us at Free Support Forums.

Related Post

Create, Edit or Convert Excel Files