When viewing many tables with complex data, In order to find the required data groups quickly, This data group often needs to be classified, A simple and fast way is to fill the background color of the cell where the data group is located, In this way, we can see the data classification intuitively when reading the file, Beautiful and practical. For developers, I'm here to provide a simple and fast way, Instant useC# Set background color for cells. Here's how I do it, For reference. I used a nameSpire.XLS
for .
NET Free components of, The product is made up ofE-iceblue Company release, Details can be found on the official website(https://www.e-iceblue.cn/Introduce/Spire-XLS-NET.html), No nonsense, Let me demonstrate my operation.

The original document is as shown in the figure:



 

step:

1, Add namespace
Using Spire.Xls; Using System.Drawing;
2,  Initialize workbook and load document
Workbook workbook = new Workbook(); workbook.LoadFromFile(@"
C:\Users\Administrator\Desktop\test.xlsx", ExcelVersion.Version97to2003);
Worksheet worksheet= workbook.Worksheets[0];Worksheet worksheet =
workbook.Worksheets[0];
3, Set cell background color
worksheet.Range["A1:C2"].Style.Color = Color.LightSeaGreen; worksheet.Range["
A3:C4"].Style.Color = Color.LightYellow; worksheet.Range["A5:C19"].Style.Color =
Color.SpringGreen;
worksheet.Range["A20:C21"].Style.Color = Color.DeepSkyBlue; worksheet.Range["
A22:C23"].Style.Color = Color.Yellow;
4, Save and preview file
workbook.SaveToFile("GradesRank.xls",ExcelVersion.Version97to2003;
System.Diagnostics.Process.Start(workbook.FileName);
The document background color is added successfully after the above steps are completed, As shown in the figure below:



 

Complete code:

C#
using System.Drawing; using Spire.Xls; namespace background_color { class
Program {static void Main(string[] args) { Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx",
ExcelVersion.Version97to2003); Worksheet worksheet= workbook.Worksheets[0]; //
set the backgroundcolor of Range["A1:C2"] worksheet.Range["A1:C2"].Style.Color =
Color.LightSeaGreen;//set the backgroundcolor of Range["A3:C4"]
worksheet.Range["A3:C4"].Style.Color = Color.LightYellow; //set the
backgroundcolor of Range["A5:C19"] worksheet.Range["A5:C19"].Style.Color =
Color.SpringGreen;//set the backgroundcolor of Range["A20:C21"] worksheet.Range[
"A20:C21"].Style.Color = Color.DeepSkyBlue; //set the backgroundcolor of
Range["A22:C23"] worksheet.Range["A22:C23"].Style.Color = Color.Yellow; //save
and launch the project workbook.SaveToFile("Sample.xls",
ExcelVersion.Version97to2003);
System.Diagnostics.Process.Start(workbook.FileName); } } }
VB.NET:
Imports System.Drawing Imports Spire.Xls Namespace background_color Class
Program Private Shared Sub Main(ByVal args() As String) Dim workbook As Workbook
= New Workbook workbook.LoadFromFile("C:\Users\Administrator\Desktop\test.xlsx"
, ExcelVersion.Version97to2003) Dim worksheet As Worksheet= workbook.Worksheets(
0) 'set the backgroundcolor of Range["A1:C2"] worksheet.Range("A1:C2"
).Style.Color = Color.LightSeaGreen 'set the backgroundcolor of Range["A3:C4"]
worksheet.Range("A3:C4").Style.Color = Color.LightYellow 'set the
backgroundcolor of Range["A5:C19"] worksheet.Range("A5:C19").Style.Color =
Color.SpringGreen'set the backgroundcolor of Range["A20:C21"] worksheet.Range("
A20:C21").Style.Color = Color.DeepSkyBlue 'set the backgroundcolor of
Range["A22:C23"] worksheet.Range("A22:C23").Style.Color = Color.Yellow 'save
and launch the project workbook.SaveToFile("Sample.xls",
ExcelVersion.Version97to2003)
System.Diagnostics.Process.Start(workbook.FileName) End Sub End Class End
Namespace