This example demonstrates how to apply formatting to a cells using Interior property.
[C#]
using System;
using System.Drawing;
using NativeExcel;
namespace Console_Interior
{
class Program
{
static void Main(string[] args)
{
string FileName = "console-interior.xls";
CreateWorkbook(FileName);
OpenWorkbookWithExcel(FileName);
}
static void CreateWorkbook(string FileName) {
//Create a new workbook
IWorkbook book = NativeExcel.Factory.CreateWorkbook();
//Add worksheet
IWorksheet sheet = book.Worksheets.Add();
int row = 2;
sheet.Cells[row, 1].Value = "xlPatternNone";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternNone;
row++;
sheet.Cells[row, 1].Value = "xlPatternSolid";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternSolid;
row++;
sheet.Cells[row, 1].Value = "xlPatternGray50";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternGray50;
row++;
sheet.Cells[row, 1].Value = "xlPatternGray75";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternGray75;
row++;
sheet.Cells[row, 1].Value = "xlPatternGray25";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternGray25;
row++;
sheet.Cells[row, 1].Value = "xlPatternHorizontal";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternHorizontal;
row++;
sheet.Cells[row, 1].Value = "xlPatternVertical";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternVertical;
row++;
sheet.Cells[row, 1].Value = "xlPatternDown";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternDown;
row++;
sheet.Cells[row, 1].Value = "xlPatternUp";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternUp;
row++;
sheet.Cells[row, 1].Value = "xlPatternChecker";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternChecker;
row++;
sheet.Cells[row, 1].Value = "xlPatternSemiGray75";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternSemiGray75;
row++;
sheet.Cells[row, 1].Value = "xlPatternLightHorizontal";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternLightHorizontal;
row++;
sheet.Cells[row, 1].Value = "xlPatternLightVertical";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternLightVertical;
row++;
sheet.Cells[row, 1].Value = "xlPatternLightDown";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternLightDown;
row++;
sheet.Cells[row, 1].Value = "xlPatternLightUp";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternLightUp;
row++;
sheet.Cells[row, 1].Value = "xlPatternGrid";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternGrid;
row++;
sheet.Cells[row, 1].Value = "xlPatternCrissCross";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternCrissCross;
row++;
sheet.Cells[row, 1].Value = "xlPatternGray16";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternGray16;
row++;
sheet.Cells[row, 1].Value = "xlPatternGray8";
sheet.Cells[row, 2, row, 10].Interior.Pattern = XlPattern.xlPatternGray8;
//Set columns' width
sheet.UsedRange.Columns.ColumnWidth = 5;
//Set width of the first column
sheet.Cells.Columns[1].ColumnWidth = 25;
//Set rows' height
sheet.UsedRange.Rows.RowHeight = 20;
sheet.UsedRange.VerticalAlignment = XlVAlign.xlVAlignCenter;
IRange range = sheet.Range[3, 2, row, 10];
//Set the background color of column 2
range.Columns[2].Interior.Color = Color.Red;
//Set the background color of column 3
range.Columns[3].Interior.Color = Color.Green;
//Set the background color of column 4
range.Columns[4].Interior.Color = Color.Blue;
//Set the background color of column 5
range.Columns[5].Interior.Color = Color.Yellow;
//Set the pattern color of column 6
range.Columns[6].Interior.PatternColor = Color.Red;
//Set the pattern color of column 7
range.Columns[7].Interior.PatternColor = Color.Green;
//Set the pattern color of column 8
range.Columns[8].Interior.PatternColor = Color.Blue;
//Set the pattern color of column 9
range.Columns[9].Interior.PatternColor = Color.Yellow;
//Add worksheet 2
IWorksheet sheet2 = book.Worksheets.Add();
//Copy range from Sheet1 to Sheet2
sheet.Cells[2,1,row,10].Copy(sheet2.Cells[2,1,row,10]);
//Set rows' height
sheet2.UsedRange.Rows.RowHeight = 20;
//Remove the interior pattern from range D7:H12
sheet2.Range["D7:H12"].Interior.Pattern = XlPattern.xlPatternNone;
//Save workbook
book.SaveAs(FileName);
}
static void OpenWorkbookWithExcel(string FileName){
try {
System.Diagnostics.Process.Start(FileName);
} catch {
Console.WriteLine(FileName + " created in application folder");
}
}
}
}
[Visual Basic]
imports System
imports NativeExcel
imports System.Drawing
Module Console_Interior
Sub Main()
Dim FileName As String = "console-interior.xls"
CreateWorkbook(FileName)
OpenWorkbookWithExcel(FileName)
End Sub
Sub CreateWorkbook(FileName As String)
'Create a new workbook
Dim book As IWorkbook = NativeExcel.Factory.CreateWorkbook()
'Add worksheet
Dim sheet As IWorksheet = book.Worksheets.Add()
Dim row As Integer = 2
sheet.Cells(row, 1).Value = "xlPatternNone"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternNone
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternSolid"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternSolid
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternGray50"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternGray50
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternGray75"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternGray75
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternGray25"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternGray25
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternHorizontal"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternHorizontal
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternVertical"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternVertical
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternDown"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternDown
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternUp"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternUp
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternChecker"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternChecker
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternSemiGray75"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternSemiGray75
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternLightHorizontal"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternLightHorizontal
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternLightVertical"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternLightVertical
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternLightDown"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternLightDown
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternLightUp"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternLightUp
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternGrid"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternGrid
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternCrissCross"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternCrissCross
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternGray16"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternGray16
row = row + 1
sheet.Cells(row, 1).Value = "xlPatternGray8"
sheet.Cells(row, 2, row, 10).Interior.Pattern = XlPattern.xlPatternGray8
'Set columns width
sheet.UsedRange.Columns.ColumnWidth = 5
'Set width of the first column
sheet.Cells.Columns(1).ColumnWidth = 25
'Set rows height
sheet.UsedRange.Rows.RowHeight = 20
sheet.UsedRange.VerticalAlignment = XlVAlign.xlVAlignCenter
Dim range As IRange = sheet.Range(3, 2, row, 10)
'background color for the column 2
range.Columns(2).Interior.Color = Color.Red
'background color for the column 3
range.Columns(3).Interior.Color = Color.Green
'background color for the column 4
range.Columns(4).Interior.Color = Color.Blue
'background color for the column 5
range.Columns(5).Interior.Color = Color.Yellow
'pattern color for the column 6
range.Columns(6).Interior.PatternColor = Color.Red
'pattern color for the column 7
range.Columns(7).Interior.PatternColor = Color.Green
'pattern color for the column 8
range.Columns(8).Interior.PatternColor = Color.Blue
'pattern color for the column 9
range.Columns(9).Interior.PatternColor = Color.Yellow
'Add worksheet 2
Dim sheet2 As IWorksheet = book.Worksheets.Add()
'Copy range from Sheet1 to Sheet2
sheet.Cells(2,1,row,10).Copy(sheet2.Cells(2,1,row,10))
'Set rows height
sheet2.UsedRange.Rows.RowHeight = 20
'Remove interior pattern from D7:H12 range
sheet2.Range("D7:H12").Interior.Pattern = XlPattern.xlPatternNone
'Save workbook
book.SaveAs(FileName)
End Sub
Sub OpenWorkbookWithExcel(FileName As String)
Try
System.Diagnostics.Process.Start(FileName)
Catch
Console.WriteLine(FileName + " created in application folder")
End Try
End Sub
End Module