News Category

Set Excel Background Color in C#, VB.NET

2012-01-29 03:37:33 Written by  support iceblue
Rate this item
(0 votes)

Excel background color is very useful for developers to analyze data information. Setting background color of rows or columns in a similar category makes the data obviously shown. In this section, one line of code will help you easily set your excel background color in a fast way by a .NET Excel component.

Spire.XLS for .NET is an Excel document generating, reading, writing and manipulating component for .NET. It enables you to set excel background color by the class Spire.Xls.Worksheet. Range[].Style.Color. While before setting the background color, you need to use Workbook.LoadFromFile(string fileName, bool preserveMode) method to load your Excel file from system and save the excel file by Workbook.SaveToFile(string fileName) after.

Please Download Spire.XLS for .NET and preview the target excel as picture below:

Set Excel Background Color

[C#]
using System.Drawing;
using Spire.Xls;

namespace background_color
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\Backgroundcolor.xls", ExcelVersion.Version97to2003);
            Worksheet worksheet = workbook.Worksheets[0];

            //set the backgroundcolor of Range["A1:D1"]
            worksheet.Range["A1:D1"].Style.Color = Color.LightSeaGreen;

            //set the backgroundcolor of Range["A2:D6"]
            worksheet.Range["A2:D6"].Style.Color = Color.SpringGreen;

            //set the backgroundcolor of Range["A7:D11"]
            worksheet.Range["A7:D11"].Style.Color = Color.DeepSkyBlue;

            //set the backgroundcolor of Range["A12:D16"]
            worksheet.Range["A12:D16"].Style.Color = Color.Yellow;

            //save and launch the project
            workbook.SaveToFile("test.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("..\Backgroundcolor.xls", ExcelVersion.Version97to2003)
            Dim worksheet As Worksheet = workbook.Worksheets(0)
            'set the backgroundcolor of Range["A1:D1"]
            worksheet.Range("A1:D1").Style.Color = Color.LightSeaGreen
            'set the backgroundcolor of Range["A2:D6"]
            worksheet.Range("A2:D6").Style.Color = Color.SpringGreen
            'set the backgroundcolor of Range["A7:D11"]
            worksheet.Range("A7:D11").Style.Color = Color.DeepSkyBlue
            'set the backgroundcolor of Range["A12:D16"]
            worksheet.Range("A12:D16").Style.Color = Color.Yellow
            'save and launch the project
            workbook.SaveToFile("test.xls", ExcelVersion.Version97to2003)
            System.Diagnostics.Process.Start(workbook.FileName)
        End Sub
    End Class
End Namespace

Spire.XLS allows user to operate Excel document directly such as save to stream, save as web response, copy, lock/unlock worksheet, set up workbook properties, etc. As a professional .NET/Silverlight Excel component, it owns the ability of inserting content into Excel document, formatting cells and converting Excel documents to popular office file formats. Spire.XLS for .NET supports Excel 97-2003, Excel 2007 and Excel 2010.

Additional Info

  • tutorial_title: Set Excel Background Color
Last modified on Friday, 12 April 2024 01:00