How to set the background color for Excel Chart in C#?
4 Nov 20253 minutes to read
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 property of the IFill 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.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/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;
#region Save
//Saving the workbook
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
#endregion
}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");
}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 UsingA complete working example to set the background color for Excel Chart using C# is present on this GitHub page.