diff --git a/FAQ/Chart/.NET/Set background color for chart/README.md b/FAQ/Chart/.NET/Set background color for chart/README.md new file mode 100644 index 00000000..8af6f34c --- /dev/null +++ b/FAQ/Chart/.NET/Set background color for chart/README.md @@ -0,0 +1,47 @@ +# How to set the background color for Excel Chart in C#? + +Step 1: Create a New C# Console Application Project. + +Step 2: Name the Project. + +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). + +Step 4: Include the following namespaces in the **Program.cs** file. + +```csharp +using System; +using System.IO; +using Syncfusion.Drawing; +using Syncfusion.XlsIO; +``` + +Step 5: Include the below code snippet in **Program.cs** to set the background color for Excel Chart in C#. +```csharp +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(); +} +``` \ No newline at end of file diff --git a/FAQ/Chart/.NET/Set background color for chart/Set background color for chart.sln b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart.sln new file mode 100644 index 00000000..31f6cea9 --- /dev/null +++ b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35506.116 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +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}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2C4D725C-E58B-4F1B-9A98-FA36512C1343}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2C4D725C-E58B-4F1B-9A98-FA36512C1343}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2C4D725C-E58B-4F1B-9A98-FA36512C1343}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2C4D725C-E58B-4F1B-9A98-FA36512C1343}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Data/InputTemplate.xlsx b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Data/InputTemplate.xlsx new file mode 100644 index 00000000..3c1edd6f Binary files /dev/null and b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Data/InputTemplate.xlsx differ diff --git a/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Output/.gitkeep b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Program.cs b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Program.cs new file mode 100644 index 00000000..42b312d8 --- /dev/null +++ b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Program.cs @@ -0,0 +1,43 @@ +using System; +using System.IO; +using Syncfusion.Drawing; +using Syncfusion.XlsIO; + +namespace Set_Background_Color_For_Chart +{ + class Program + { + static void Main(string[] args) + { + 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(); + } + + } + + } +} diff --git a/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Set background color for chart.csproj b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Set background color for chart.csproj new file mode 100644 index 00000000..39fca8f8 --- /dev/null +++ b/FAQ/Chart/.NET/Set background color for chart/Set background color for chart/Set background color for chart.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + Set_background_color_for_chart + enable + enable + + + + + + + + + Always + + + +