Excel Text Color in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source


The sample demonstrates how to design text color in excel spreadsheet.



using System.Drawing;
using Spire.Xls;

namespace TextColor
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\..\..\Data\parts.xls",ExcelVersion.Version97to2003);
            Worksheet worksheet = workbook.Worksheets[0];

            //set the text color of Range["A1"]
            worksheet.Range["A2:G3"].Style.Font.Color = Color.Red;

            //set the text color of Range["A2"]
            worksheet.Range["A4:G4"].Style.Font.Color = Color.Violet;

            //set the text color of Range["A3"]
            worksheet.Range["A6:D8"].Style.Font.Color = Color.Blue;

            //set the text color of Range["A4"]
            worksheet.Range["F9:G12"].Style.Font.Color = Color.Black;

            //set the text color of Range["B1"]
            worksheet.Range["A13:C16"].Style.Font.Color = Color.Green;

            //set the text color of Range["B2"]
            worksheet.Range["E13:G13"].Style.Font.Color = Color.Purple;

            //set the text color of Range["B3"]
            worksheet.Range["E14:G14"].Style.Font.Color = Color.Plum;

            //set the text color of Range["B4"]
            worksheet.Range["A15:G18"].Style.Font.Color = Color.Peru;
           
            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Imports System.Drawing
Imports Spire.Xls

Namespace TextColor
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim workbook As New Workbook()
			workbook.LoadFromFile("..\..\..\..\Data\parts.xls",ExcelVersion.Version97to2003)
			Dim worksheet As Worksheet = workbook.Worksheets(0)

			'set the text color of Range["A1"]
			worksheet.Range("A2:G3").Style.Font.Color = Color.Red

			'set the text color of Range["A2"]
			worksheet.Range("A4:G4").Style.Font.Color = Color.Violet

			'set the text color of Range["A3"]
			worksheet.Range("A6:D8").Style.Font.Color = Color.Blue

			'set the text color of Range["A4"]
			worksheet.Range("F9:G12").Style.Font.Color = Color.Black

			'set the text color of Range["B1"]
			worksheet.Range("A13:C16").Style.Font.Color = Color.Green

			'set the text color of Range["B2"]
			worksheet.Range("E13:G13").Style.Font.Color = Color.Purple

			'set the text color of Range["B3"]
			worksheet.Range("E14:G14").Style.Font.Color = Color.Plum

			'set the text color of Range["B4"]
			worksheet.Range("A15:G18").Style.Font.Color = Color.Peru

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