How to Set Excel Background Color in WPF

Excel background color is a very useful property when we need to demonstrate data for special purpose. That is also a basic feature for any Excel solution developer. Here will demonstrate how to design Excel background color regardless whether Microsoft Excel is installed on your system or not. Spire.Xls for WPF is a professional component for directly managing Excel operations. With it you can easily and fast set Excel background color in WPF.

Spire.Xls for WPF is always welcome to any kind of trial and evaluation. So now please feel free to download Spire.Xls for WPF and then follow our guide to easily set Excel background color in WPF or try other function of Spire.Xls for WPF.

Friendly Reminder:

  • Before we code to set Excel background color in WPF, please add spire.xls dll as reference by Clicking ProjectAdd ReferenceBrowse → Choose the folder contains Spire.XLS for WPFBin.NET 4.0.
  • Please make sure the namespace-Spire.Xls being imported.

Next we will demonstrate the how to set Excel background color in WPF within just 3 Steps.

Step 1: Load Excel in WPF

Spire.XLS for WPF enables users directly loading Excel file or create a new Excel. We will load an Excel in this step and later set its background color in WPF. Please check the code examples below.

[C#]
Workbook workbook = new Workbook(); 
workbook.LoadFromFile(@"..\test.xls", ExcelVersion.Version97to2003);
[VB.NET]
Dim workbook As New Workbook()
workbook.LoadFromFile("..\test.xls", ExcelVersion.Version97to2003)

Step 2: Set Excel background color in WPF

In this step, we will set the background color of the Excel which we just loaded above. With Spire.Xls for WPF, developers only need few lines of codes to freely set background colors in Excel. Please check the following example.

[C#]
Worksheet worksheet = workbook.Worksheets[0];
//set the backgroundcolor=LightBlue of Range["A2:E9"]
worksheet.Range["A2:E9"].Style.Color = Color.LightBlue;
//set the backgroundcolor=Silver of Range["A11:E18"]
worksheet.Range["A11:E18"].Style.Color = Color.Silver;
[VB.NET]
Dim worksheet As Worksheet = workbook.Worksheets(0)
'set the backgroundcolor=LightBlue of Range["A2:E9"]
worksheet.Range("A2:E9").Style.Color = Color.LightBlue
'set the backgroundcolor=Silver of Range["A11:E18"]
worksheet.Range("A11:E18").Style.Color = Color.Silver

Step 3: Save and preview

Now we will use the following codes to save and preview our work.

[C#]
workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
System.Diagnostics.Process.Start(workbook.FileName);
[VB.NET]
workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
System.Diagnostics.Process.Start(workbook.FileName)

We have successfully set Excel background color in WPF, now press F5 to run this application and you will see the different background color we set. Below is an effective screenshot, please have a check: