Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Spreadsheet .NET
>
Tutorial
> Set Cell Fill Color
The following example shows you how to set cell fill color.
C# example
using System; using Independentsoft.Office; using Independentsoft.Office.Spreadsheet; using Independentsoft.Office.Spreadsheet.Styles; namespace Sample { class Program { static void Main(string[] args) { Fill grayFill = new Fill(); grayFill.Pattern = new PatternFill(); grayFill.Pattern.Type = PatternType.Solid; grayFill.Pattern.ForegroundColor = new ForegroundColor(); grayFill.Pattern.ForegroundColor.Theme = 0; grayFill.Pattern.ForegroundColor.Tint = -0.34998626667073579; grayFill.Pattern.BackgroundColor = new BackgroundColor(); grayFill.Pattern.BackgroundColor.Color = "FF000000"; //black color for cell text Fill lightGrayFill = new Fill(); lightGrayFill.Pattern = new PatternFill(); lightGrayFill.Pattern.Type = PatternType.Solid; lightGrayFill.Pattern.ForegroundColor = new ForegroundColor(); lightGrayFill.Pattern.ForegroundColor.Theme = 0; lightGrayFill.Pattern.ForegroundColor.Tint = -0.049989318521683403; lightGrayFill.Pattern.BackgroundColor = new BackgroundColor(); lightGrayFill.Pattern.BackgroundColor.Color = "FF000000"; //black color for cell text CellFormat format1 = new CellFormat(); format1.Fill = grayFill; format1.ApplyFill = true; //important CellFormat format2 = new CellFormat(); format2.Fill = lightGrayFill; format2.ApplyFill = true; //important Cell a1 = new Cell(100); a1.Format = format1; Cell b1 = new Cell(200); b1.Format = format2; Cell c1 = new Cell(); c1.Formula = new Formula("SUM(A1,B1)"); Worksheet sheet1 = new Worksheet(); sheet1["A1"] = a1; sheet1["B1"] = b1; sheet1["C1"] = c1; Workbook book = new Workbook(); book.Sheets.Add(sheet1); book.Save("c:\\test\\output.xlsx", true); } } }
VB example
Imports System Imports Independentsoft.Office Imports Independentsoft.Office.Spreadsheet Imports Independentsoft.Office.Spreadsheet.Styles Module Module1 Sub Main(ByVal args As String()) Dim grayFill As New Fill() grayFill.Pattern = New PatternFill() grayFill.Pattern.Type = PatternType.Solid grayFill.Pattern.ForegroundColor = New ForegroundColor() grayFill.Pattern.ForegroundColor.Theme = 0 grayFill.Pattern.ForegroundColor.Tint = -0.349986266670736 grayFill.Pattern.BackgroundColor = New BackgroundColor() grayFill.Pattern.BackgroundColor.Color = "FF000000" 'black color for cell text Dim lightGrayFill As New Fill() lightGrayFill.Pattern = New PatternFill() lightGrayFill.Pattern.Type = PatternType.Solid lightGrayFill.Pattern.ForegroundColor = New ForegroundColor() lightGrayFill.Pattern.ForegroundColor.Theme = 0 lightGrayFill.Pattern.ForegroundColor.Tint = -0.0499893185216834 lightGrayFill.Pattern.BackgroundColor = New BackgroundColor() lightGrayFill.Pattern.BackgroundColor.Color = "FF000000" 'black color for cell text Dim format1 As New CellFormat() format1.Fill = grayFill format1.ApplyFill = True 'important Dim format2 As New CellFormat() format2.Fill = lightGrayFill format2.ApplyFill = True 'important Dim a1 As New Cell(100) a1.Format = format1 Dim b1 As New Cell(200) b1.Format = format2 Dim c1 As New Cell() c1.Formula = New Formula("SUM(A1,B1)") Dim sheet1 As New Worksheet() sheet1("A1") = a1 sheet1("B1") = b1 sheet1("C1") = c1 Dim book As New Workbook() book.Sheets.Add(sheet1) book.Save("c:\test\output.xlsx", True) End Sub End Module
Need help? Ask our developers:
Name*
Email*
Message*