Create Excel Chart
IronXL supports the creation and editing of charts for Excel documents in the modern XLSX file format.
This example shows how to create a Line Chart. Other chart types are also supported.
How to Create Excel Line Chart in C#
- Install the Excel library to create an Excel line chart.
- Load an Excel workbook and create a chart using
CreateChart
. - Add more values to the
Workbook
as needed. - Set the chart's title and legend, and call the
Plot
method to display the chart. - Save the
Workbook
data to the Excel file.
using IronXL;
using System;
class Program
{
static void Main()
{
// Load an existing workbook or create a new one
WorkBook workbook = WorkBook.Load("Sample.xlsx");
// Access the first worksheet
WorkSheet sheet = workbook.DefaultWorkSheet;
// Adding values to cells for the chart
sheet["A1"].Value = "Category";
sheet["B1"].Value = "Values";
sheet["A2"].Value = "Category 1";
sheet["B2"].Value = 10;
sheet["A3"].Value = "Category 2";
sheet["B3"].Value = 20;
sheet["A4"].Value = "Category 3";
sheet["B4"].Value = 30;
// Create a new line chart
var chart = sheet.CreateChart(ChartType.Line);
// Set the data range for the chart
chart.SetData(sheet["A1:B4"]);
// Set chart title and legend
chart.Title = "Sample Line Chart";
chart.ShowLegend = true;
// Plot the chart within the worksheet
chart.Plot();
// Save the changes to the workbook
workbook.SaveAs("Sample_With_Chart.xlsx");
Console.WriteLine("Excel file with line chart is created.");
}
}
using IronXL;
using System;
class Program
{
static void Main()
{
// Load an existing workbook or create a new one
WorkBook workbook = WorkBook.Load("Sample.xlsx");
// Access the first worksheet
WorkSheet sheet = workbook.DefaultWorkSheet;
// Adding values to cells for the chart
sheet["A1"].Value = "Category";
sheet["B1"].Value = "Values";
sheet["A2"].Value = "Category 1";
sheet["B2"].Value = 10;
sheet["A3"].Value = "Category 2";
sheet["B3"].Value = 20;
sheet["A4"].Value = "Category 3";
sheet["B4"].Value = 30;
// Create a new line chart
var chart = sheet.CreateChart(ChartType.Line);
// Set the data range for the chart
chart.SetData(sheet["A1:B4"]);
// Set chart title and legend
chart.Title = "Sample Line Chart";
chart.ShowLegend = true;
// Plot the chart within the worksheet
chart.Plot();
// Save the changes to the workbook
workbook.SaveAs("Sample_With_Chart.xlsx");
Console.WriteLine("Excel file with line chart is created.");
}
}
Imports IronXL
Imports System
Friend Class Program
Shared Sub Main()
' Load an existing workbook or create a new one
Dim workbook As WorkBook = WorkBook.Load("Sample.xlsx")
' Access the first worksheet
Dim sheet As WorkSheet = workbook.DefaultWorkSheet
' Adding values to cells for the chart
sheet("A1").Value = "Category"
sheet("B1").Value = "Values"
sheet("A2").Value = "Category 1"
sheet("B2").Value = 10
sheet("A3").Value = "Category 2"
sheet("B3").Value = 20
sheet("A4").Value = "Category 3"
sheet("B4").Value = 30
' Create a new line chart
Dim chart = sheet.CreateChart(ChartType.Line)
' Set the data range for the chart
chart.SetData(sheet("A1:B4"))
' Set chart title and legend
chart.Title = "Sample Line Chart"
chart.ShowLegend = True
' Plot the chart within the worksheet
chart.Plot()
' Save the changes to the workbook
workbook.SaveAs("Sample_With_Chart.xlsx")
Console.WriteLine("Excel file with line chart is created.")
End Sub
End Class
The above C# program demonstrates how to use IronXL to create a line chart in an Excel workbook. The code performs the following steps:
- Load a Workbook: Use
WorkBook.Load()
to load an existing Excel file or create a new one if it doesn't exist. - Access a Worksheet: Use the default worksheet in the workbook to enter data.
- Enter Data: Manually enter data that will be used to plot the chart.
- Create a Chart: Initialize a new line chart using
CreateChart
with the specifiedChartType
. - Define Data Range: Use
SetData
to specify the range of cells containing the data for the chart. - Customize Chart: Add a title and display the legend for clarification.
- Plot and Save: Call
Plot()
to insert the chart into the worksheet and save the updates to the Excel file. - Inform: Output confirmation that the process is completed.
Make sure to install the IronXL package via NuGet to run this code successfully.