Skip to content

Commit 3f49879

Browse files
261689-ChartBackgroundColorExample
1 parent 5d30f95 commit 3f49879

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# How to set the background color for Excel Chart in C#?
2+
3+
Step 1: Create a New C# Console Application Project.
4+
5+
Step 2: Name the Project.
6+
7+
Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).
8+
9+
Step 4: Include the following namespaces in the **Program.cs** file.
10+
11+
```csharp
12+
using System;
13+
using System.IO;
14+
using Syncfusion.Drawing;
15+
using Syncfusion.XlsIO;
16+
```
17+
18+
Step 5: Include the below code snippet in **Program.cs** to set the background color for Excel Chart in C#.
19+
```csharp
20+
using (ExcelEngine excelEngine = new ExcelEngine())
21+
{
22+
IApplication application = excelEngine.Excel;
23+
application.DefaultVersion = ExcelVersion.Xlsx;
24+
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
25+
IWorkbook workbook = application.Workbooks.Open(inputStream);
26+
IWorksheet worksheet = workbook.Worksheets[0];
27+
28+
//Get the first chart in the worksheet
29+
IChartShape chart = worksheet.Charts[0];
30+
31+
//Applying background color for plot area
32+
chart.PlotArea.Fill.ForeColor = Color.LightYellow;
33+
34+
//Applying background color for chart area
35+
chart.ChartArea.Fill.ForeColor = Color.LightGreen;
36+
37+
#region Save
38+
//Saving the workbook
39+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
40+
workbook.SaveAs(outputStream);
41+
#endregion
42+
43+
//Dispose streams
44+
outputStream.Dispose();
45+
inputStream.Dispose();
46+
}
47+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Set background color for chart", "Set background color for chart\Set background color for chart.csproj", "{2C4D725C-E58B-4F1B-9A98-FA36512C1343}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2C4D725C-E58B-4F1B-9A98-FA36512C1343}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2C4D725C-E58B-4F1B-9A98-FA36512C1343}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2C4D725C-E58B-4F1B-9A98-FA36512C1343}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2C4D725C-E58B-4F1B-9A98-FA36512C1343}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Output/.gitkeep

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.IO;
3+
using Syncfusion.Drawing;
4+
using Syncfusion.XlsIO;
5+
6+
namespace Set_Background_Color_For_Chart
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (ExcelEngine excelEngine = new ExcelEngine())
13+
{
14+
IApplication application = excelEngine.Excel;
15+
application.DefaultVersion = ExcelVersion.Xlsx;
16+
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
17+
IWorkbook workbook = application.Workbooks.Open(inputStream);
18+
IWorksheet worksheet = workbook.Worksheets[0];
19+
20+
//Get the first chart in the worksheet
21+
IChartShape chart = worksheet.Charts[0];
22+
23+
//Applying background color for plot area
24+
chart.PlotArea.Fill.ForeColor = Color.LightYellow;
25+
26+
//Applying background color for chart area
27+
chart.ChartArea.Fill.ForeColor = Color.LightGreen;
28+
29+
#region Save
30+
//Saving the workbook
31+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
32+
workbook.SaveAs(outputStream);
33+
#endregion
34+
35+
//Dispose streams
36+
outputStream.Dispose();
37+
inputStream.Dispose();
38+
}
39+
40+
}
41+
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Set_background_color_for_chart</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)