C #/VB. NET: Set the background color of an Excel table,
When you view many tables with complex data, to quickly find the desired data group, you often need to classify the data group, A simple and quick way is to fill in the background color of the cell where the data group is located, so that we can intuitively see the data classification while reading the file, which is both beautiful and practical. For developers, here I provide a simple and fast way to set the background color for Cells Using C. The following is my operation method for your reference. Here I use a Spire. XLS. NET free version of components, the product by E-iceblue company release, specific details can go to the official website to understand (https://www.e-iceblue.cn/Introduce/Spire-XLS-NET.html), no nonsense, below I demonstrate my operations.
Original file
Steps:
1,Add namespace
Using Spire.Xls;Using System.Drawing;
2,Initialize the workbook and load the 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 the file
workbook.SaveToFile("GradesRank.xls",ExcelVersion.Version97to2003;System.Diagnostics.Process.Start(workbook.FileName);
After completing the preceding steps, the background color of the document is added, as shown in:
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.DrawingImports Spire.XlsNamespace 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 ClassEnd Namespace