How to create an Excel chart with a trendline using C#, VB.NET?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
This article explains how to create an Excel chart with a trendline using
Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Use the following code snippet for creating an Excel chart with a trendline.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet sheet = workbook.Worksheets[0];
//Create a chart
IChartShape chart = sheet.Charts.Add();
//Set chart type
chart.ChartType = ExcelChartType.Column_Clustered;
//Set data range in the worksheet
chart.DataRange = sheet.Range["A1:B6"];
chart.IsSeriesInRows = false;
//Set chart title
chart.ChartTitle = "Sales Details";
//Set axis titles
chart.PrimaryValueAxis.Title = "Amount(in $)";
chart.PrimaryCategoryAxis.Title = "Items";
//Set trendline for the chart series
IChartSerie serie1 = chart.Series[0];
serie1.TrendLines.Add(ExcelTrendLineType.Linear);
serie1.TrendLines[0].Border.LinePattern = ExcelChartLinePattern.DashDot;
serie1.TrendLines[0].Border.LineColor = Syncfusion.Drawing.Color.FromArgb(68, 114, 196);
//Set legend
chart.HasLegend = false;
//Position the chart in the worksheet
chart.TopRow = 8;
chart.LeftColumn = 1;
chart.BottomRow = 23;
chart.RightColumn = 8;
//Save the workbook
workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
}
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
Dim sheet As IWorksheet = workbook.Worksheets(0)
'Create a chart
Dim chart As IChartShape = sheet.Charts.Add()
'Set chart type
chart.ChartType = ExcelChartType.Column_Clustered
'Set data range in the worksheet
chart.DataRange = sheet.Range("A1:B6")
chart.IsSeriesInRows = False
'Set chart title
chart.ChartTitle = "Sales Details"
'Set axis titles
chart.PrimaryValueAxis.Title = "Amount(in $)"
chart.PrimaryCategoryAxis.Title = "Items"
'Set trendline for the chart series
Dim serie1 As IChartSerie = chart.Series(0)
serie1.TrendLines.Add(ExcelTrendLineType.Linear)
serie1.TrendLines(0).Border.LinePattern = ExcelChartLinePattern.DashDot
serie1.TrendLines(0).Border.LineColor = Color.FromArgb(68, 114, 196)
'Set legend
chart.HasLegend = False
'Position the chart in the worksheet
chart.TopRow = 8
chart.LeftColumn = 1
chart.BottomRow = 23
chart.RightColumn = 8
'Save the workbook
workbook.SaveAs("Output.xlsx")
End Using
You can get the complete sample for creating an Excel chart with a trendline from
I hope you enjoyed learning about how to create an Excel chart with a trendline using
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!