Set Cell Fill in Excel in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to set cell fill in Excel workbook via Spire.XLS.

using Spire.Xls;
using System.Drawing;

namespace CellFill
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a workbook
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            
            //set the color of Range["C3"] 
            sheet.Range["B3:B4"].Style.Color = Color.Yellow;
            sheet.Range["B3:B4"].Style.FillPattern = ExcelPatternType.Angle;

            //set the fillpattern of Range["D4"]
            sheet.Range["C6:C7"].Style.Color = Color.GreenYellow;
            sheet.Range["C6:C7"].Style.FillPattern = ExcelPatternType.ThickDiagonalCrosshatch;

            //set the fillpattern of Range["E5"]
            sheet.Range["D9:D10"].Style.Color = Color.BlueViolet;
            sheet.Range["D9:D10"].Style.FillPattern = ExcelPatternType.Vertical;

            //set the fillpattern of Range["A1:C2"] 
            sheet.Range["E12:F15"].Style.Color = Color.LightSkyBlue;
            sheet.Range["E12:F15"].Style.FillPattern = ExcelPatternType.ForwardDiagonal;

            //set the fillpattern of Range["G9:H9"]
            sheet.Range["G17:I21"].Style.Color = Color.Bisque;
            sheet.Range["G17:I21"].Style.FillPattern = ExcelPatternType.Percent25Gray;

            //save the workbook
            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Imports Spire.Xls
Imports System.Drawing

Namespace CellFill
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			'create a workbook
			Dim workbook As New Workbook()
			Dim sheet As Worksheet = workbook.Worksheets(0)

			'set the color of Range["C3"] 
			sheet.Range("B3:B4").Style.Color = Color.Yellow
			sheet.Range("B3:B4").Style.FillPattern = ExcelPatternType.Angle

			'set the fillpattern of Range["D4"]
			sheet.Range("C6:C7").Style.Color = Color.GreenYellow
			sheet.Range("C6:C7").Style.FillPattern = ExcelPatternType.ThickDiagonalCrosshatch

			'set the fillpattern of Range["E5"]
			sheet.Range("D9:D10").Style.Color = Color.BlueViolet
			sheet.Range("D9:D10").Style.FillPattern = ExcelPatternType.Vertical

			'set the fillpattern of Range["A1:C2"] 
			sheet.Range("E12:F15").Style.Color = Color.LightSkyBlue
			sheet.Range("E12:F15").Style.FillPattern = ExcelPatternType.ForwardDiagonal

			'set the fillpattern of Range["G9:H9"]
			sheet.Range("G17:I21").Style.Color = Color.Bisque
			sheet.Range("G17:I21").Style.FillPattern = ExcelPatternType.Percent25Gray

			'save the workbook
			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
	End Class
End Namespace