Skip to content

261689-How to set the background color for Excel chart using XlsIO #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: hotfix/hotfix-v29.1.33
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Document-Processing-toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,9 @@
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-show-the-leader-line-on-Excel-chart">How to show the leader line on Excel chart?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-set-the-background-color-for-Excel-chart-in-C#">How to set the background color for Excel Chart in C#?</a>
</li>
</ul>
</li>
</ul>
Expand Down
3 changes: 2 additions & 1 deletion Document-Processing/Excel/Excel-Library/NET/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,5 @@ The frequently asked questions in Essential<sup>&reg;</sup> XlsIO are listed bel
* [How to get the list of worksheet names in an Excel workbook?](faqs/how-to-get-the-list-of-worksheet-names-in-an-Excel-workbook)
* [How to switch chart series data interpretation from horizontal (rows) to vertical (columns) in Excel?](faqs/how-to-switch-chart-series-data-interpretation-from-horizontal-(rows)-to-vertical-(columns)-in-Excel)
* [How to add Oval shape to Excel chart using XlsIO?](faqs/how-to-add-oval-shape-to-Excel-chart)
* [How to show the leader line on Excel chart?](faqs/how-to-show-the-leader-line-on-Excel-chart)
* [How to show the leader line on Excel chart?](faqs/how-to-show-the-leader-line-on-Excel-chart)
* [How to set the background color for Excel Chart in C#?](faqs/how-to-set-the-background-color-for-Excel-chart-in-C#)
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
title: How to set the background color for Excel Chart in C# | Syncfusion
description: This page explains how to set the background color for the chart using Syncfusion .NET Excel library (XlsIO).
platform: document-processing
control: XlsIO
documentation: UG
---

# How to set the background color for Excel Chart in C#?

You can set the background color of a chart by customizing either the plot area or the chart area. XlsIO allows you to set the background color for both the PlotArea and ChartArea using the [ForeColor](https://help.syncfusion.com/cr/document-processing/Syncfusion.XlsIO.IFill.html#Syncfusion_XlsIO_IFill_ForeColor) property of the [IFill](https://help.syncfusion.com/cr/document-processing/Syncfusion.XlsIO.IFill.html) interface.


The following code example illustrates how to set the background color for Excel Chart in C# (cross-platform and Windows-specific) and VB.NET.

{% tabs %}
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Chart/.NET/Set%20background%20color%20for%20chart/Set%20background%20color%20for%20chart/Program.cs,180" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];

//Get the first chart in the worksheet
IChartShape chart = worksheet.Charts[0];

//Applying background color for plot area
chart.PlotArea.Fill.ForeColor = Color.LightYellow;

//Applying background color for chart area
chart.ChartArea.Fill.ForeColor = Color.LightGreen;

#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion

//Dispose streams
outputStream.Dispose();
inputStream.Dispose();
}
{% endhighlight %}

{% highlight c# tabtitle="C# [Windows-specific]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];

//Get the first chart in the worksheet
IChartShape chart = worksheet.Charts[0];

//Applying background color for plot area
chart.PlotArea.Fill.ForeColor = Color.LightYellow;

//Applying background color for chart area
chart.ChartArea.Fill.ForeColor = Color.LightGreen;

//Saving the workbook
workbook.SaveAs("Output.xlsx");
}
{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
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 worksheet As IWorksheet = workbook.Worksheets(0)

'Get the first chart in the worksheet
Dim chart As IChartShape = worksheet.Charts(0)

'Applying background color for plot area
chart.PlotArea.Fill.ForeColor = Color.Yellow

'Applying background color for chart area
chart.ChartArea.Fill.ForeColor = Color.LightGreen

'Save the workbook
workbook.SaveAs("Output.xlsx")
End Using
{% endhighlight %}
{% endtabs %}

A complete working example to set the background color for Excel Chart using C# is present on [this GitHub page](https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Chart/.NET/Set%20background%20color%20for%20chart).