Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, you will learn how to create and edit Excel charts using the Iron XL library within a C# application. Start by ensuring the Iron XL NuGet package is installed in your project. Import the Iron XL namespace in your program's CS file and set the license key. Load an Excel workbook using the Workbook.Load
method, and retrieve the default worksheet for chart creation. Create a column chart with the worksheet.CreateChart
method, specifying its position and size. Add series to the chart and customize the title and legend position. Save the chart as 'columnchart.xlsx'. You can also create line and pie charts by specifying chart types. To edit existing charts, load them from a file, adjust properties like legend position and title, and save the changes. Additionally, charts can be removed using the RemoveChart
method. By following these steps, you can efficiently create, customize, and edit Excel charts in your C# applications using Iron XL. Subscribe for more tutorials from Iron Software and explore the power of Iron XL by signing up for a trial and downloading the package.
Here is an example of how to create a column chart:
using IronXL; // Import the Iron XL namespace
class Program
{
static void Main()
{
// Set your IronXL license key
IronXL.License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
// Load the Excel workbook
WorkBook workbook = WorkBook.Load("example.xlsx");
// Retrieve the first worksheet
WorkSheet worksheet = workbook.DefaultWorkSheet;
// Create a column chart
var chart = worksheet.CreateChart(ChartType.ColumnChart);
// Set chart position and size
chart.Left = 100;
chart.Top = 50;
chart.Width = 600;
chart.Height = 400;
// Add data series to the chart
chart.AddSeries(worksheet["A1:A10"], worksheet["B1:B10"]);
// Customize the chart's title and legend
chart.SetTitle("Sales Data");
chart.SetLegendPosition(ChartLegendPosition.Bottom);
// Save the changes to a new Excel file
workbook.SaveAs("columnchart.xlsx");
Console.WriteLine("Chart created and saved as columnchart.xlsx");
}
}
using IronXL; // Import the Iron XL namespace
class Program
{
static void Main()
{
// Set your IronXL license key
IronXL.License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
// Load the Excel workbook
WorkBook workbook = WorkBook.Load("example.xlsx");
// Retrieve the first worksheet
WorkSheet worksheet = workbook.DefaultWorkSheet;
// Create a column chart
var chart = worksheet.CreateChart(ChartType.ColumnChart);
// Set chart position and size
chart.Left = 100;
chart.Top = 50;
chart.Width = 600;
chart.Height = 400;
// Add data series to the chart
chart.AddSeries(worksheet["A1:A10"], worksheet["B1:B10"]);
// Customize the chart's title and legend
chart.SetTitle("Sales Data");
chart.SetLegendPosition(ChartLegendPosition.Bottom);
// Save the changes to a new Excel file
workbook.SaveAs("columnchart.xlsx");
Console.WriteLine("Chart created and saved as columnchart.xlsx");
}
}
Imports IronXL ' Import the Iron XL namespace
Friend Class Program
Shared Sub Main()
' Set your IronXL license key
IronXL.License.LicenseKey = "YOUR_LICENSE_KEY_HERE"
' Load the Excel workbook
Dim workbook As WorkBook = WorkBook.Load("example.xlsx")
' Retrieve the first worksheet
Dim worksheet As WorkSheet = workbook.DefaultWorkSheet
' Create a column chart
Dim chart = worksheet.CreateChart(ChartType.ColumnChart)
' Set chart position and size
chart.Left = 100
chart.Top = 50
chart.Width = 600
chart.Height = 400
' Add data series to the chart
chart.AddSeries(worksheet("A1:A10"), worksheet("B1:B10"))
' Customize the chart's title and legend
chart.SetTitle("Sales Data")
chart.SetLegendPosition(ChartLegendPosition.Bottom)
' Save the changes to a new Excel file
workbook.SaveAs("columnchart.xlsx")
Console.WriteLine("Chart created and saved as columnchart.xlsx")
End Sub
End Class
Further Reading: How to Create and Edit Excel Charts in C#