Cómo crear y editar gráficos de 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

Preguntas Frecuentes

¿Cómo puedo crear un gráfico de Excel programáticamente usando C#?

Puedes crear un gráfico de Excel programáticamente usando C# al utilizar la biblioteca IronXL. Primero, instala IronXL a través del Administrador de Paquetes NuGet en Visual Studio, carga un archivo de Excel existente en un objeto Workbook, y utiliza el método CreateChart para definir el tipo y ubicación del gráfico. Agrega series de datos, establece títulos y guarda el Workbook en el archivo Excel.

¿Qué pasos se involucran en la creación de un gráfico de Excel programáticamente?

Los pasos para crear un gráfico de Excel programáticamente incluyen: instalar IronXL, cargar un archivo de Excel existente en un objeto Workbook, usar el método CreateChart para crear el gráfico, agregar series de datos, establecer el título y la leyenda del gráfico, y guardar el libro de trabajo.

¿Qué tipos de gráficos se pueden crear con IronXL?

IronXL soporta la creación de varios tipos de gráficos como gráficos de columnas, de barras, de líneas y de pastel programáticamente.

¿Cómo puedo agregar una serie de datos a un gráfico de Excel usando C#?

Para agregar una serie de datos a un gráfico de Excel usando C#, utiliza el método AddSeries de IronXL. Especifica el rango para los datos del eje x y del eje y, y opcionalmente establece un título para la serie.

¿Cómo personalizo la posición de la leyenda de un gráfico de Excel programáticamente?

Puedes personalizar la posición de la leyenda de un gráfico de Excel usando IronXL al utilizar el método SetLegendPosition. Especifica posiciones como Abajo, Arriba, Izquierda o Derecha.

¿Qué espacios de nombres son necesarios para trabajar con gráficos de Excel en IronXL?

Para trabajar con gráficos de Excel usando IronXL, incluye los espacios de nombres necesarios: IronXL y IronXL.Drawing.Charts.

¿Cómo puedo guardar un archivo de Excel después de agregar un gráfico programáticamente?

Después de agregar un gráfico programáticamente, guarda el archivo de Excel usando el método SaveAs de IronXL del objeto Workbook. Especifica la ruta y el nombre de archivo deseados para guardarlo.

¿Se puede usar IronXL para modificar archivos de Excel existentes?

Sí, IronXL puede ser usado para cargar, editar y guardar archivos de Excel existentes. Te permite modificar datos, agregar gráficos y realizar otras operaciones programáticamente.

¿Dónde puedo encontrar documentación para usar IronXL con gráficos de Excel?

Puedes acceder a la documentación de referencia del API de IronXL en su sitio web para obtener más información sobre sus características y cómo usarlas con gráficos de Excel.

Curtis Chau
Escritor Técnico

Curtis Chau tiene una licenciatura en Ciencias de la Computación (Carleton University) y se especializa en el desarrollo front-end con experiencia en Node.js, TypeScript, JavaScript y React. Apasionado por crear interfaces de usuario intuitivas y estéticamente agradables, disfruta trabajando con frameworks modernos y creando manuales bien ...

Leer más
¿Listo para empezar?
Nuget Descargas 1,686,155 | Versión: 2025.11 recién lanzado