Comment créer et modifier des graphiques Excel en C#

How to Create and Edit Excel Charts in C#

This article was translated from English: Does it need improvement?
Translated
View the article in English

In Excel, a chart is a graphical representation of data. It is a visual tool used to display and analyze data in a more understandable and meaningful way. Excel provides a variety of chart types, such as bar charts, line charts, pie charts, and more, each of which is suited for different types of data and analysis.

Quickstart: Create and Plot a Line Chart in Seconds

With IronXL, you can install, load a workbook, call CreateChart, add your data series, set your title & legend position, and Plot—all in just a few lines. This example shows how quickly you can go from no chart to a polished visual using native C# methods—without any Interop overhead.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot();
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


Get started with IronXL

Commencez à utiliser IronXL dans votre projet aujourd'hui avec un essai gratuit.

Première étape :
green arrow pointer


Create Charts Example

IronXL supports column, scatter, line, pie, bar, and area charts. To create a chart, we have to specify a few things separately.

  1. Start by using the CreateChart method to specify the type of chart and its location in the worksheet.
  2. Add the series with the AddSeries method. This method also accepts a single column of data since that is sufficient for some chart types. The first parameter represents values for the horizontal axis. The second parameter represents values for the vertical axis.
  3. Optionally, specify the series name, chart name, and legend position.
  4. Invoke the Plot method to plot the chart. The method will plot the chart using all the data that was added. Multiple calls to this method lead to plotting multiple charts instead of modifying the existing chart.

Let's create some charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below:

Data

Column Chart

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-column-chart.cs
using IronXL;
using IronXL.Drawing.Charts;

WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);

string xAxis = "A2:A7";

// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;

// Add the series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;

// Add the series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;

// Set the chart title
chart.SetTitle("Column Chart");

// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);

// Plot the chart
chart.Plot();

workBook.SaveAs("columnChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts

Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)

Private xAxis As String = "A2:A7"

' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue

' Add the series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue

' Add the series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue

' Set the chart title
chart.SetTitle("Column Chart")

' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)

' Plot the chart
chart.Plot()

workBook.SaveAs("columnChart.xlsx")
$vbLabelText   $csharpLabel
Column chart

Line Chart

Since a line chart can represent as much information as a column chart, switching between the two is very simple. You only need to change the chart type.

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-line-chart.cs
using IronXL;
using IronXL.Drawing.Charts;

WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);

string xAxis = "A2:A7";

// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;

// Add the series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;

// Add the series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;

// Set the chart title
chart.SetTitle("Line Chart");

// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);

// Plot the chart
chart.Plot();

workBook.SaveAs("lineChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts

Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)

Private xAxis As String = "A2:A7"

' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue

' Add the series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue

' Add the series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue

' Set the chart title
chart.SetTitle("Line Chart")

' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)

' Plot the chart
chart.Plot()

workBook.SaveAs("lineChart.xlsx")
$vbLabelText   $csharpLabel
Line chart

Pie Chart

For a pie chart, only one column of data is needed.

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs
using IronXL;
using IronXL.Drawing.Charts;

WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);

string xAxis = "A2:A7";

// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;

// Set the chart title
chart.SetTitle("Pie Chart");

// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);

// Plot the chart
chart.Plot();

workBook.SaveAs("pieChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts

Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)

Private xAxis As String = "A2:A7"

' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue

' Set the chart title
chart.SetTitle("Pie Chart")

' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)

' Plot the chart
chart.Plot()

workBook.SaveAs("pieChart.xlsx")
$vbLabelText   $csharpLabel
Pie chart

Edit Chart Example

There are a few things you can edit in an existing chart. You can edit the legend position and chart title. To edit the chart, you must first retrieve the chart by accessing the Charts property and select the targeted chart for editing. From there, access the properties of the chart to make your edits.

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs
using IronXL;
using IronXL.Drawing.Charts;

WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Retrieve the chart
IChart chart = workSheet.Charts[0];

// Edit the legend position
chart.SetLegendPosition(LegendPosition.Top);

// Edit the chart title
chart.SetTitle("Edited Chart");

workBook.SaveAs("editedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts

Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Retrieve the chart
Private chart As IChart = workSheet.Charts(0)

' Edit the legend position
chart.SetLegendPosition(LegendPosition.Top)

' Edit the chart title
chart.SetTitle("Edited Chart")

workBook.SaveAs("editedChart.xlsx")
$vbLabelText   $csharpLabel
Before
After

Remove Chart Example

To remove an existing chart from a spreadsheet, first retrieve the chart from the Charts property of the worksheet object. You will receive a list of charts from the Charts property. Pass the targeted chart object to the RemoveChart method to remove it.

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
using System.Collections.Generic;

WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Retrieve the chart
List<IChart> chart = workSheet.Charts;

// Remove the chart
workSheet.RemoveChart(chart[0]);

workBook.SaveAs("removedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Imports System.Collections.Generic

Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Retrieve the chart
Private chart As List(Of IChart) = workSheet.Charts

' Remove the chart
workSheet.RemoveChart(chart(0))

workBook.SaveAs("removedChart.xlsx")
$vbLabelText   $csharpLabel

Questions Fréquemment Posées

Comment puis-je créer des graphiques Excel en C# sans utiliser Interop ?

Vous pouvez créer des graphiques Excel en C# sans Interop en utilisant la bibliothèque IronXL. Commencez par télécharger la bibliothèque depuis NuGet, puis utilisez des méthodes telles que CreateChart pour spécifier le type de graphique et son emplacement dans la feuille de calcul.

Quelles étapes sont impliquées pour personnaliser les graphiques Excel en utilisant C# ?

Pour personnaliser des graphiques Excel en utilisant C#, préparez d'abord vos données, puis utilisez la méthode AddSeries pour ajouter des séries de données. Personnalisez les propriétés du graphique comme les titres et les positions de légende en accédant au graphique via la propriété Charts de la feuille de calcul.

Comment puis-je passer d'un type de graphique à un autre en C# ?

Passer d'un type de graphique à un autre en C# est simple. Changez simplement le paramètre de type de graphique dans la méthode CreateChart pour passer entre les graphiques en colonne, en dispersion, en ligne, en secteur, en barre ou en aire.

Quelle est la méthode pour enregistrer les modifications après avoir édité un graphique Excel en C# ?

Après avoir édité un graphique Excel, utilisez la méthode SaveAs pour enregistrer les modifications dans le classeur, en garantissant que toutes les modifications sont préservées.

Puis-je supprimer un graphique existant d'une feuille de calcul Excel en utilisant C# ?

Oui, vous pouvez supprimer un graphique existant en le récupérant via la propriété Charts de la feuille de calcul et en utilisant ensuite la méthode RemoveChart pour le supprimer.

Quels sont les avantages d'utiliser IronXL pour la création de graphiques dans les applications .NET ?

IronXL permet une création et une édition efficaces de graphiques dans les applications .NET sans dépendre de l'Interop Excel, ce qui améliore les performances et réduit la complexité. Il prend en charge divers types de graphiques et fournit des méthodes pour une personnalisation facile.

Comment puis-je tracer un graphique en C# après avoir ajouté des séries de données ?

Après avoir ajouté des séries de données en utilisant la méthode AddSeries, vous pouvez tracer le graphique en appelant la méthode Plot, qui génère le graphique à partir des données fournies.

Est-il possible de modifier les propriétés d'un graphique en C# après qu'il ait été créé ?

Oui, vous pouvez modifier les propriétés d'un graphique en C# en y accédant via la propriété Charts de la feuille de calcul et en modifiant des attributs comme le titre du graphique, la position de la légende, etc.

Chaknith Bin
Ingénieur logiciel
Chaknith travaille sur IronXL et IronBarcode. Il a une expertise approfondie en C# et .NET, aidant à améliorer le logiciel et à soutenir les clients. Ses idées issues des interactions avec les utilisateurs contribuent à de meilleurs produits, documentation et expérience globale.
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir