Comment créer et éditer des graphiques Excel en C# | IronXL

How to Use C# to Create Excel Charts

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

The following How-To enables you to create an Excel chart programmatically in C# using IronXL.

Quickstart: Programmatically Generate an Excel Chart in C#)

This example shows you how easy it is to spin up a column chart in IronXL: use a single worksheet method to create the chart, add a data series, set a title and legend, plot it, and save—get meaningful visuals in minutes, not hours.

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.

    IChart chart = worksheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
    chart.AddSeries("A2:A7", "B2:B7");
    chart.SetTitle("Sales Overview").SetLegendPosition(LegendPosition.Bottom).Plot();
    workbook.SaveAs("SalesChart.xlsx");
  3. Deploy to test on your live environment

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

as-heading:3(Minimal Workflow (6 steps)

  1. Install Excel library to create Excel charts.
  2. Load the existing Excel file into a Workbook object.
  3. Create a chart with CreateChart.
  4. Set the chart's title and legend
  5. Call the Plot method.
  6. Save the Workbook to the Excel file.

Programmatically Create Excel Charts in .NET

  • Create Excel graphs programmatically
  • Add series with title and legend
How To Work related to How to Use C# to Create Excel Charts

Step 1

1. Install IronXL

First, the easiest way to install IronXL is to make use of the NuGet Package manager in Visual Studio:

  • Select the Project menu
  • Manage NuGet Packages
  • Search for IronXL.Excel
  • Install

You could also enter the following command into the Developer Command Prompt:

Install-Package IronXL.Excel

Or Download from here: https://ironsoftware.com/csharp/excel/packages/IronXL.zip


How to Tutorial

2. Create Excel Chart for .NET

Now for the project!

Add the following details into an Excel Spreadsheet. This is shown below:

Data to be used for charting

Figure 1 Data to be used for charting

Add the Namespaces necessary to work with Excel charts in IronXL.

using IronXL;
using IronXL.Drawing.Charts;
using IronXL;
using IronXL.Drawing.Charts;
Imports IronXL
Imports IronXL.Drawing.Charts
$vbLabelText   $csharpLabel

Add code to create the Excel graph programmatically with IronXL:

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

// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("Chart_Ex.xlsx");
// Use the default worksheet from the workbook
WorkSheet ws = wb.DefaultWorkSheet;

// Create a column chart at the specified range of cells
var chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20);

// Define the range for the x-axis data
const string xAxis = "A2:A7";

// Add a series for the chart using data in the range and give it a title from the first row
var series = chart.AddSeries(xAxis, "B2:B7");
series.Title = ws["B1"].StringValue;

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

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

// Set the chart title
chart.SetTitle("Column Chart");
// Position the legend at the bottom of the chart
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart with the provided data
chart.Plot();
// Save the workbook with the newly added chart
wb.SaveAs("Exported_Column_Chart.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

A Workbook object and a Worksheet object are created. The CreateChart method of the Worksheet object gets called to specify the chart type and chart location. The chart’s series get added, with its Title and the Legend. This is shown below.

Chart output

Figure 2Chart output


Library Quick Access

IronXL API Reference Documentation

Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL API Reference Documentation.

IronXL API Reference Documentation
Documentation related to 2. Create Excel Chart for .NET

Questions Fréquemment Posées

Comment puis-je créer un graphique Excel par programmation en utilisant C# ?

Vous pouvez créer un graphique Excel par programmation en utilisant C# en utilisant la bibliothèque IronXL. Tout d'abord, installez IronXL via le gestionnaire de packages NuGet dans Visual Studio, chargez un fichier Excel existant dans un objet Workbook, et utilisez la méthode CreateChart pour définir le type et l'emplacement du graphique. Ajoutez des séries de données, définissez des titres, et sauvegardez le Workbook dans le fichier Excel.

Quelles sont les étapes de la création d'un graphique Excel par programmation ?

Les étapes pour créer un graphique Excel par programmation incluent : l'installation d'IronXL, le chargement d'un fichier Excel existant dans un objet Workbook, l'utilisation de la méthode CreateChart pour créer le graphique, l'ajout de séries de données, la définition du titre et de la légende du graphique, et la sauvegarde du workbook.

Quels types de graphiques peuvent être créés avec IronXL ?

IronXL prend en charge la création de divers types de graphiques tels que les graphiques en colonnes, en barres, linéaires et en secteurs par programmation.

Comment puis-je ajouter une série de données à un graphique Excel en utilisant C# ?

Pour ajouter une série de données à un graphique Excel en utilisant C#, utilisez la méthode AddSeries d'IronXL. Spécifiez les plages de données pour les axes x et y, et éventuellement définissez un titre pour la série.

Comment puis-je personnaliser la position de la légende d'un graphique Excel par programmation ?

Vous pouvez personnaliser la position de la légende d'un graphique Excel en utilisant IronXL via la méthode SetLegendPosition. Indiquez des positions comme Bas, Haut, Gauche, ou Droite.

Quels espaces de noms sont nécessaires pour travailler avec les graphiques Excel dans IronXL ?

Pour travailler avec les graphiques Excel en utilisant IronXL, incluez les espaces de noms nécessaires : IronXL et IronXL.Drawing.Charts.

Comment puis-je enregistrer un fichier Excel après avoir ajouté un graphique par programmation ?

Après avoir ajouté un graphique par programmation, enregistrez le fichier Excel en utilisant la méthode SaveAs d'IronXL de l'objet Workbook. Spécifiez le chemin et le nom de fichier souhaités pour l'enregistrement.

IronXL peut-il être utilisé pour modifier des fichiers Excel existants ?

Oui, IronXL peut être utilisé pour charger, éditer et enregistrer des fichiers Excel existants. Il vous permet de modifier des données, d'ajouter des graphiques, et d'effectuer d'autres opérations par programmation.

Où puis-je trouver la documentation pour l'utilisation d'IronXL avec les graphiques Excel ?

Vous pouvez accéder à la documentation de référence de l'API d'IronXL sur leur site web pour plus d'informations sur ses fonctionnalités et comment les utiliser avec les graphiques Excel.

Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir