How to Use C#/VB.NET Add Cell Fill Color/Pattern in Excel

Why Add Cell Fill Color/Pattern?

To make a better appearance or emphasize some content in cells we can add cell fill color or pattern. According to different requirements, we can fill cell with different colors and patterns. Microsoft Excel gives us a lot of choices on cell fill. We can only add cell fill with color, or just pattern, or add both color and pattern together in cell. Most of time, it gives good impressions and very useful.

Add Cell Fill in Microsoft Excel

Open your excel file, select the cell which you want to add cell fill. Right click to choose format menu and go to cell option. Choose pattern and you can find there are many kinds of colors and patterns on the dialog box. Then, you can add cell fill at your will.

How to Use C#/VB.NET Add Cell Fill Color/Patter?

Spire.XLS for .NET is .NET Excel component which enables your .NET applications fast generate, read, write and modify Excel document without Microsoft Office Excel Automation. With Spire.XLS, we can add cell fill with Color/Pattern via C#/VB.NET, and we don’t need Microsoft Excel installed on system.

In Spire.XLS, we can use sheet.Range["A1"].Style.Color method to add cell fill color in cell. While using sheet.Range["A1"].Style.FillPattern, we can add cell fill with pattern in cell. The following sample will show us how to add cell fill with color, add cell fill with pattern and add cell fill with both color and pattern.

C# Cell Fill Color/Pattern

01     using System.Drawing;
02     using Spire.Xls;
03    
04     namespace Hyperlink
05     {
06         class Program
07         {
08             static void Main(string[] args)
09             {
10                 //Create a new workbook.
11                 Workbook workbook = new Workbook();
12                 Worksheet sheet = workbook.Worksheets[0];
13                  
14                 //Write a common text.
15                 sheet.Range["B2"].Text = "Home page";
16                 
17                 //Write a text with background color.
18                 sheet.Range["B4"].Text = "Home page";
19                 sheet.Range["B4"].Style.Color = Color.Green;
20                 
21                 //Write a text to the cell with pattern filled in.
22                 sheet.Range["B6"].Text = "Home page";
23                 sheet.Range["B6"].Style.FillPattern = ExcelPatternType.Percent125Gray;
24                  
25                 //Write a text with background color and pattern filled in.
26                 sheet.Range["B8"].Text = "Home page";
27                 sheet.Range["B8"].Style.Color = Color.Green;
28                 sheet.Range["B8"].Style.FillPattern = ExcelPatternType.Percent125Gray;
29                 sheet.AutoFitColumn(2);
30                 
31                 //Save the file.
32                 workbook.SaveToFile("Sample.xls");
33                 
34                 //Launch the file.
35                 System.Diagnostics.Process.Start("Sample.xls");
36             }
37         }
38     }
 
VB.NET Cell Fill Color/Pattern
01     Imports Microsoft.VisualBasic
02     Imports System.Drawing
03     Imports Spire.Xls
04    
05     Module Module1
06    
07         Sub Main()
08             'Create a new workbook.
09             Dim workbook As Workbook = New Workbook()
10             Dim sheet As Worksheet = workbook.Worksheets(0)
11             
12             'Write a common text.
13             sheet.Range["B2"].Text = "Home page";
14             
15             'Write a text with background color.
16             sheet.Range["B4"].Text = "Home page";
17             sheet.Range["B4"].Style.Color = Color.Green;
18             
19             'Write a text to the cell with pattern filled in.
20             sheet.Range["B6"].Text = "Home page";
21             sheet.Range["B6"].Style.FillPattern = ExcelPatternType.Percent125Gray;
22             
23             'Write a text with background color and pattern filled in.
24             sheet.Range["B8"].Text = "Home page";
25             sheet.Range["B8"].Style.Color = Color.Green;
26             sheet.Range["B8"].Style.FillPattern = ExcelPatternType.Percent125Gray;
27             sheet.AutoFitColumn(2);
28             
29             'Save the file.
30             workbook.SaveToFile("Sample.xls")
31             
32             'Launch the file.
33             System.Diagnostics.Process.Start("Sample.xls")
34         End Sub
35     End Module

 

After running the sample code above in your application, you will get the result like this:

 

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 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. Click to learn more…

Download Spire.XLS

Purchase Spire.XLS

Leave a Reply