How to convert Syncfusion's WinForms chart to Excel chart using XlsIO
This article explains how to convert Windows Chart to Excel Chart using XlsIO in C#/VB.NET.
How to convert Windows Chart to Excel Chart?
Windows Chart is an easily configurable, presentation-quality business chart control. XlsIO supports creating and modifying Excel charts inside a workbook. We can convert the Essential Chart to an Excel Chart using XlsIO by retrieving the data from the Essential Chart control and providing the necessary values to the Excel Chart.
The screenshot below shows the Syncfusion Windows Chart.
Windows Chart
To convert windows chart to Excel chart, you need to follow the below steps.
Steps to convert Windows Chart to Excel Chart:
- We need chart data to create a chart in Excel document using XlsIO. So, first we need to get chart data from windows chart control.
// Getting X and Y axis data and saving in Excel document
worksheet.Range[i,1].Number = this.windowsChart.Series[0].Points[i-1].X;
worksheet.Range[i,2].Number = this.windowsChart.Series[0].Points[i-1].YValues[0];
- The Excel chart is created with the given data.
// Create a chart worksheet
IChart chart = workbook.Charts.Add("Essential Chart");
// Specify the title of the Chart
chart.ChartTitle = windowsChart.Text;
chart.ChartTitleArea.Size = windowsChart.Font.Size;
chart.PrimaryCategoryAxis.Title = windowsChart.PrimaryYAxis.Title;
chart.PrimaryValueAxis.Title = windowsChart.PrimaryXAxis.Title;
// Initialize a new series instance and add it to the series collection of the chart
IChartSerie series = chart.Series.Add();
// Specify the chart type of the series
series.SerieType = ExcelChartType.Bar_Stacked;
// Specify the name of the series. This will be displayed as the text of the legend
series.Name = "Sales Performance";
// Specify the value ranges for the series
series.Values = worksheet.Range["B1:B5"];
// Specify the category labels for the series
series.CategoryLabels = worksheet.Range["A1:A5"];
// Legend settings
chart.Legend.TextArea.Size = windowsChart.Legend.Font.Size * 1.5;
chart.Legend.X = 2400;
chart.Legend.Y = 750;
// Grid lines
chart.PrimaryCategoryAxis.HasMajorGridLines = windowsChart.PrimaryYAxis.DrawGrid;
- The chart control skin styles can be used for Excel chart by getting equivalent chart area and plot area styles.
To learn more about creating Excel Charts using XlsIO, please refer to the documentation.
The following C#/VB.NET complete code snippet shows how to convert Syncfusion Windows Chart to Excel Chart using XlsIO.
// Instantiate the spreadsheet creation engine
ExcelEngine excelEngine = new ExcelEngine();
// Instantiate the Excel application object
IApplication application = excelEngine.Excel;
// The workbook is created
IWorkbook workbook = application.Workbooks.Create(1);
// The first worksheet object in the worksheets collection is accessed
IWorksheet worksheet = workbook.Worksheets[0];
// Getting data points from chart control
for (int i = 1; i <= windowsChart.Series[0].Points.Count; i++)
{
worksheet.Range[i, 1].Number = windowsChart.Series[0].Points[i - 1].X;
worksheet.Range[i, 2].Number = windowsChart.Series[0].Points[i - 1].YValues[0];
}
// Create a chart worksheet
IChart chart = workbook.Charts.Add("Essential Chart");
// Specify the title of the Chart
chart.ChartTitle = windowsChart.Text;
chart.ChartTitleArea.Size = windowsChart.Font.Size;
chart.PrimaryCategoryAxis.Title = windowsChart.PrimaryYAxis.Title;
chart.PrimaryValueAxis.Title = windowsChart.PrimaryXAxis.Title;
// Initialize a new series instance and add it to the series collection of the chart
IChartSerie series = chart.Series.Add();
// Specify the chart type of the series
series.SerieType = ExcelChartType.Bar_Stacked;
// Specify the name of the series. This will be displayed as the text of the legend
series.Name = "Sales Performance";
// Specify the value ranges for the series
series.Values = worksheet.Range["B1:B5"];
//Specify the category labels for the series
series.CategoryLabels = worksheet.Range["A1:A5"];
// Legend settings
chart.Legend.TextArea.Size = windowsChart.Legend.Font.Size * 1.5;
chart.Legend.X = 2400;
chart.Legend.Y = 750;
// Grid lines
chart.PrimaryCategoryAxis.HasMajorGridLines = windowsChart.PrimaryYAxis.DrawGrid;
// Make the chart the active sheet
chart.Activate();
// Format Series
series.SerieFormat.Fill.ForeColor = windowsChart.Series[0].BackColor;
// Set chart area styles
if (windowsChart.BackInterior.GradientColors.Count > 0)
{
if (windowsChart.BackInterior.GradientColors.Count == 1)
{
chart.ChartArea.Fill.FillType = ExcelFillType.Gradient;
chart.ChartArea.Fill.GradientStyle = GetXlsIOStyle(windowsChart.BackInterior.GradientStyle);
chart.ChartArea.Fill.GradientColorType = ExcelGradientColor.OneColor;
chart.ChartArea.Fill.ForeColor = windowsChart.BackInterior.ForeColor;
}
if (windowsChart.BackInterior.GradientColors.Count == 2)
{
chart.ChartArea.Fill.FillType = ExcelFillType.Gradient;
chart.ChartArea.Fill.GradientStyle = GetXlsIOStyle(windowsChart.BackInterior.GradientStyle);
chart.ChartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chart.ChartArea.Fill.BackColor = windowsChart.BackInterior.BackColor;
chart.ChartArea.Fill.ForeColor = windowsChart.BackInterior.ForeColor;
}
if (windowsChart.BackInterior.GradientColors.Count > 2)
{
chart.ChartArea.Fill.FillType = ExcelFillType.Gradient;
chart.ChartArea.Fill.GradientStyle = GetXlsIOStyle(windowsChart.BackInterior.GradientStyle);
chart.ChartArea.Fill.FillType = ExcelFillType.Gradient;
chart.ChartArea.Fill.GradientColorType = ExcelGradientColor.MultiColor;
GradientStops result = new GradientStops();
GradientStopImpl stop1;
int colorStart = 0, colorStop = 10000;
foreach (System.Drawing.Color color in windowsChart.BackInterior.GradientColors)
{
stop1 = new GradientStopImpl(color, colorStart, colorStop);
(chart.ChartArea.Fill as ShapeFillImpl).GradientStops.Add(stop1);
colorStart = colorStop;
colorStop += 10000;
}
}
}
// Set plot area styles
if (windowsChart.ChartInterior.GradientColors.Count > 0)
{
if (windowsChart.ChartInterior.GradientColors.Count == 1)
{
chart.PlotArea.Fill.FillType = ExcelFillType.Gradient;
chart.PlotArea.Fill.GradientStyle = GetXlsIOStyle(windowsChart.ChartInterior.GradientStyle);
chart.PlotArea.Fill.GradientColorType = ExcelGradientColor.OneColor;
chart.PlotArea.Fill.ForeColor = windowsChart.ChartInterior.ForeColor;
}
if (windowsChart.ChartInterior.GradientColors.Count == 2)
{
chart.PlotArea.Fill.FillType = ExcelFillType.Gradient;
chart.PlotArea.Fill.GradientStyle = GetXlsIOStyle(windowsChart.ChartInterior.GradientStyle);
chart.PlotArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chart.PlotArea.Fill.BackColor = windowsChart.ChartInterior.BackColor;
chart.PlotArea.Fill.ForeColor = windowsChart.ChartInterior.ForeColor;
}
if (windowsChart.ChartInterior.GradientColors.Count > 2)
{
chart.PlotArea.Fill.FillType = ExcelFillType.Gradient;
chart.PlotArea.Fill.GradientStyle = GetXlsIOStyle(windowsChart.ChartInterior.GradientStyle);
chart.PlotArea.Fill.FillType = ExcelFillType.Gradient;
chart.PlotArea.Fill.GradientColorType = ExcelGradientColor.MultiColor;
GradientStops result = new GradientStops();
GradientStopImpl stop1;
int colorStart = 0, colorStop = 10000; // Can be changed
foreach (System.Drawing.Color color in windowsChart.ChartInterior.GradientColors)
{
stop1 = new GradientStopImpl(color, colorStart, colorStop);
(chart.PlotArea.Fill as ShapeFillImpl).GradientStops.Add(stop1);
colorStart = colorStop;
colorStop += 10000;
}
}
}
workbook.SaveAs("Sample.xlsx");
workbook.Close();
// Provides XlsIO equivalent gradient style from Windows Forms chart
public ExcelGradientStyle GetXlsIOStyle(GradientStyle style)
{
ExcelGradientStyle xlsIoStyle = ExcelGradientStyle.Diagonal_Down;
switch (style)
{
case GradientStyle.Horizontal:
xlsIoStyle = ExcelGradientStyle.Vertical;
break;
case GradientStyle.Vertical:
xlsIoStyle = ExcelGradientStyle.Horizontal;
break;
case GradientStyle.ForwardDiagonal:
xlsIoStyle = ExcelGradientStyle.Diagonal_Up;
break;
case GradientStyle.BackwardDiagonal:
xlsIoStyle = ExcelGradientStyle.Diagonal_Down;
break;
case GradientStyle.PathEllipse:
xlsIoStyle = ExcelGradientStyle.From_Center;
break;
case GradientStyle.PathRectangle:
xlsIoStyle = ExcelGradientStyle.From_Corner;
break;
}
return xlsIoStyle;
}
'Instantiate the spreadsheet creation engine Dim excelEngine As ExcelEngine = New ExcelEngine 'Instantiate the excel application object Dim application As IApplication = excelEngine.Excel 'The workbook is created Dim workbook As IWorkbook = application.Workbooks.Create(1) 'The first worksheet object in the worksheets collection is accessed Dim worksheet As IWorksheet = workbook.Worksheets(0) Dim i As Integer = 1 Do While (i <= Me.windowsChart.Series(0).Points.Count) worksheet.Range(i, 1).Number = Me.windowsChart.Series(0).Points((i - 1)).X worksheet.Range(i, 2).Number = Me.windowsChart.Series(0).Points((i - 1)).YValues(0) i = (i + 1)LoopDim chart As IChart = workbook.Charts.Add("Essential Chart") ' Specify the title of the Chartchart.ChartTitle = Me.windowsChart.Text chart.ChartTitleArea.Size = windowsChart.Font.Sizechart.PrimaryCategoryAxis.Title = Me.windowsChart.PrimaryYAxis.Titlechart.PrimaryValueAxis.Title = Me.windowsChart.PrimaryXAxis.Title Dim series As IChartSerie = chart.Series.Add ' Specify the chart type of the series. series.SerieType = ExcelChartType.Bar_Stacked ' Specify the name of the series. This will be displayed as the text of the legendseries.Name = "Sales Performance" ' Specify the value ranges for the series. series.Values = worksheet.Range("B1:B5") ' Specify the Category labels for the series. series.CategoryLabels = worksheet.Range("A1:A5") ' Legend settings chart.Legend.TextArea.Size = windowsChart.Legend.Font.Size * 1.5 chart.Legend.X = 2400 chart.Legend.Y = 750 ' Grid lines chart.PrimaryCategoryAxis.HasMajorGridLines = windowsChart.PrimaryYAxis.DrawGrid ' Make the chart as active sheet chart.Activate ' Format Series series.SerieFormat.Fill.ForeColor= windowsChart.Series(0).BackColor ' Set chart area styles If (Me.windowsChart.BackInterior.GradientColors.Count > 0) Then If (Me.windowsChart.BackInterior.GradientColors.Count = 1) Then chart.ChartArea.Fill.FillType = ExcelFillType.Gradient chart.ChartArea.Fill.GradientStyle = GetXlsIOStyle(Me.windowsChart.BackInterior.GradientStyle) chart.ChartArea.Fill.GradientColorType = ExcelGradientColor.OneColor chart.ChartArea.Fill.ForeColor = Me.windowsChart.BackInterior.ForeColor End If If (Me.windowsChart.BackInterior.GradientColors.Count = 2) Then chart.ChartArea.Fill.FillType = ExcelFillType.Gradient chart.ChartArea.Fill.GradientStyle = GetXlsIOStyle(Me.windowsChart.BackInterior.GradientStyle) chart.ChartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor chart.ChartArea.Fill.BackColor = Me.windowsChart.BackInterior.BackColor chart.ChartArea.Fill.ForeColor = Me.windowsChart.BackInterior.ForeColor End If If (Me.windowsChart.BackInterior.GradientColors.Count > 2) Then chart.ChartArea.Fill.FillType = ExcelFillType.Gradient chart.ChartArea.Fill.GradientStyle = GetXlsIOStyle(Me.windowsChart.BackInterior.GradientStyle) chart.ChartArea.Fill.FillType = ExcelFillType.Gradient chart.ChartArea.Fill.GradientColorType = ExcelGradientColor.MultiColor Dim result As GradientStops = New GradientStops Dim stop1 As GradientStopImpl Dim colorStop As Integer = 10000 Dim colorStart As Integer = 0 For Each color As System.Drawing.Color In Me.windowsChart.BackInterior.GradientColors stop1 = New GradientStopImpl(color, colorStart, colorStop) CType(chart.ChartArea.Fill,ShapeFillImpl).GradientStops.Add(stop1) colorStart = colorStop colorStop = (colorStop + 10000) Next End If End If ' Set plot area stylesIf (Me.windowsChart.ChartInterior.GradientColors.Count > 0) Then If (Me.windowsChart.ChartInterior.GradientColors.Count = 1) Then chart.PlotArea.Fill.FillType = ExcelFillType.Gradient chart.PlotArea.Fill.GradientStyle = GetXlsIOStyle(Me.windowsChart.ChartInterior.GradientStyle) chart.PlotArea.Fill.GradientColorType = ExcelGradientColor.OneColor chart.PlotArea.Fill.ForeColor = Me.windowsChart.ChartInterior.ForeColor End If If (Me.windowsChart.ChartInterior.GradientColors.Count = 2) Then chart.PlotArea.Fill.FillType = ExcelFillType.Gradient chart.PlotArea.Fill.GradientStyle = GetXlsIOStyle(Me.windowsChart.ChartInterior.GradientStyle) chart.PlotArea.Fill.GradientColorType = ExcelGradientColor.TwoColor chart.PlotArea.Fill.BackColor = Me.windowsChart.ChartInterior.BackColor chart.PlotArea.Fill.ForeColor = Me.windowsChart.ChartInterior.ForeColor End If If (Me.windowsChart.ChartInterior.GradientColors.Count > 2) Then chart.PlotArea.Fill.FillType = ExcelFillType.Gradient chart.PlotArea.Fill.GradientStyle = GetXlsIOStyle(Me.windowsChart.ChartInterior.GradientStyle) chart.PlotArea.Fill.FillType = ExcelFillType.Gradient chart.PlotArea.Fill.GradientColorType = ExcelGradientColor.MultiColor Dim result As GradientStops = New GradientStops Dim stop1 As GradientStopImpl Dim colorStop As Integer = 10000 Dim colorStart As Integer = 0 For Each color As System.Drawing.Color In Me.windowsChart.ChartInterior.GradientColors stop1 = New GradientStopImpl(color, colorStart, colorStop) CType(chart.PlotArea.Fill,ShapeFillImpl).GradientStops.Add(stop1) colorStart = colorStop colorStop = (colorStop + 10000) Next End If End If workbook.SaveAs("Sample.xlsx")workbook.Close' Provides XlsIO equivalent gradient style from Windows Forms chartPublic Function GetXlsIOStyle(ByVal style As GradientStyle) As ExcelGradientStyle Dim xlsIoStyle As ExcelGradientStyle = ExcelGradientStyle.Diagonl_Down Select Case (style) Case GradientStyle.Horizontal xlsIoStyle = ExcelGradientStyle.Vertical Case GradientStyle.Vertical xlsIoStyle = ExcelGradientStyle.Horizontal Case GradientStyle.ForwardDiagonal xlsIoStyle = ExcelGradientStyle.Diagonl_Up Case GradientStyle.BackwardDiagonal xlsIoStyle = ExcelGradientStyle.Diagonl_Down Case GradientStyle.PathEllipse xlsIoStyle = ExcelGradientStyle.From_Center Case GradientStyle.PathRectangle xlsIoStyle = ExcelGradientStyle.From_Corner End Select Return xlsIoStyleEnd Function
The below screenshot shows the output generated by the above code.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to convert Syncfusion's WinForms Chart to Excel Chart using XlsIO.
You can refer to our XlsIO’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.
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 or feedback portal. We are always happy to assist you!