C#/VB.NET fills the background color of Excel cells

We know that when dealing with many tables, we will encounter a variety of complex data. When there is a lot of data, if there is no data classification, it may cost a lot to quickly identify different types of data. time, and there are bound to be mistakes. Among the many methods of classifying data, color-filling the cells in the data table can be said to be relatively simple and rude, and at the same time, it can also beautify the table, making it less boring to look at the data.

Then, in the C# language environment, how to realize the color filling of the cousin cell through code steps? The following example gives the answer! The article is reproduced from http://www.cnblogs.com/Yesi/p/7569580.html  , if you are interested, you can check the details by yourself.

 

Note : The component Spire.XLS for .NET is used in the operation. Before writing the code, you need to download and install it first, and add the reference dll file

 

Before operation:



 

After operation:


 

 

   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

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326606051&siteId=291194637