Sunday, 29 January 2012 03:37

Set Excel Background Color in C#, VB.NET

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

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);

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)
        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, 11 July 2014 02:49