In this tutorial, we will learn how to set background color in excel cell using c# step by step.
When we finish the program, we will see the following screenshot.
Step 1: Add the Excel library referece as in the following screen.
Step 2: We design the c# program as in this picture
Step 3: Add this namespace top of the Page with this alias.
1 2 3 |
using Excel = Microsoft.Office.Interop.Excel; |
Step 4: Create global variables and classes. Then set the first values in construct method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public partial class Form1 : Form { int row, col; Excel.Application ExcepApp; Excel.Workbook ExcelBook; Excel.Worksheet ExcelSheet; public Form1() { InitializeComponent(); row = 1; col = 1; ExcepApp = new Excel.Application(); ExcelBook = ExcepApp.Workbooks.Open("E:\\Book1.xls"); ExcelSheet = (Excel.Worksheet)ExcelBook.Worksheets.get_Item(1); } |
Step 5: Add those codes in button click methos that name is “Add Text”. And we set the background color in first conditional.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private void button1_Click(object sender, EventArgs e) { ExcepApp.Cells[row, col].Value = textBox1.Text; if (chbx1.Checked==true) { //Change background color ExcepApp.Cells[row, col].Interior.Color = Excel.XlRgbColor.rgbDarkOrange; } if (chbx2.Checked == true) { //change font color ExcepApp.Cells[row, col].Font.Color = Excel.XlRgbColor.rgbMidnightBlue; } row++; } |
Step 6: When we close the form, we save the Excel file and we release the Excel dependency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { ExcepApp.Visible = true; ExcelBook.Save(); ExcelBook.Close(true, null, null); ExcepApp.Quit(); try { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcepApp); ExcepApp = null; } catch (Exception ex) { ExcepApp = null; MessageBox.Show("Error " + ex.ToString()); } finally { GC.Collect(); } } |
All codes…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; namespace CsharpConsoleExamples { public partial class Form1 : Form { int row, col; Excel.Application ExcepApp; Excel.Workbook ExcelBook; Excel.Worksheet ExcelSheet; public Form1() { InitializeComponent(); row = 1; col = 1; ExcepApp = new Excel.Application(); ExcelBook = ExcepApp.Workbooks.Open("E:\\Book1.xls"); ExcelSheet = (Excel.Worksheet)ExcelBook.Worksheets.get_Item(1); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { ExcepApp.Visible = true; ExcelBook.Save(); ExcelBook.Close(true, null, null); ExcepApp.Quit(); try { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcepApp); ExcepApp = null; } catch (Exception ex) { ExcepApp = null; MessageBox.Show("Error " + ex.ToString()); } finally { GC.Collect(); } } private void button1_Click(object sender, EventArgs e) { ExcepApp.Cells[row, col].Value = textBox1.Text; if (chbx1.Checked==true) { //Change background color ExcepApp.Cells[row, col].Interior.Color = Excel.XlRgbColor.rgbDarkOrange; } if (chbx2.Checked == true) { //change font color ExcepApp.Cells[row, col].Font.Color = Excel.XlRgbColor.rgbMidnightBlue; } row++; } } } |