Set Text Font in Excel Workbook in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source


The sample demonstrates how to design text font in an excel workbook.



using Spire.Xls;

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

            //set the font of Range["A1"]
            worksheet.Range["A1"].Text = "BaTang";
            worksheet.Range["A1"].Style.Font.FontName = "BaTang";

            //set the font of Range["A2"]
            worksheet.Range["A2"].Text = "Cooper Black";
            worksheet.Range["A2"].Style.Font.FontName = "Cooper Black";
            worksheet.Range["A2"].Style.Font.Size = 14;

            //set the font of Range["A3"]
            worksheet.Range["A3"].Text = "Gulim";
            worksheet.Range["A3"].Style.Font.FontName = "Gulim";
            worksheet.Range["A3"].Style.Font.Size = 17;

            //set the font of Range["A4"]
            worksheet.Range["A4"].Text = "Freestyle Script";
            worksheet.Range["A4"].Style.Font.FontName = "Freestyle Script";
            worksheet.Range["A4"].Style.Font.Size = 17;

            //set the font of Range["A5"]
            worksheet.Range["A5"].Text = "Felix Titling";
            worksheet.Range["A5"].Style.Font.FontName = "Felix Titling";
            worksheet.Range["A5"].Style.Font.Size = 17;

            //set the font of Range["A6"]
            worksheet.Range["A6"].Text = "Eras Bold ITC";
            worksheet.Range["A6"].Style.Font.FontName = "Eras Bold ITC";
            worksheet.Range["A6"].Style.Font.Size = 17;

            //set the font of Range["A7"]
            worksheet.Range["A7"].Text = "Tempus Sans ITC";
            worksheet.Range["A7"].Style.Font.FontName = "Tempus Sans ITC";
            worksheet.Range["A7"].Style.Font.Size = 17;

            //set the font of Range["A8"]
            worksheet.Range["A8"].Text = "STENCIL";
            worksheet.Range["A8"].Style.Font.FontName = "STENCIL";
            worksheet.Range["A8"].Style.Font.Size = 17;

            worksheet.AutoFitColumn(1);

            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
  
        }
    }
}

Imports Spire.Xls

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

			'set the font of Range["A1"]
			worksheet.Range("A1").Text = "BaTang"
			worksheet.Range("A1").Style.Font.FontName = "BaTang"

			'set the font of Range["A2"]
			worksheet.Range("A2").Text = "Cooper Black"
			worksheet.Range("A2").Style.Font.FontName = "Cooper Black"
			worksheet.Range("A2").Style.Font.Size = 14

			'set the font of Range["A3"]
			worksheet.Range("A3").Text = "Gulim"
			worksheet.Range("A3").Style.Font.FontName = "Gulim"
			worksheet.Range("A3").Style.Font.Size = 17

			'set the font of Range["A4"]
			worksheet.Range("A4").Text = "Freestyle Script"
			worksheet.Range("A4").Style.Font.FontName = "Freestyle Script"
			worksheet.Range("A4").Style.Font.Size = 17

			'set the font of Range["A5"]
			worksheet.Range("A5").Text = "Felix Titling"
			worksheet.Range("A5").Style.Font.FontName = "Felix Titling"
			worksheet.Range("A5").Style.Font.Size = 17

			'set the font of Range["A6"]
			worksheet.Range("A6").Text = "Eras Bold ITC"
			worksheet.Range("A6").Style.Font.FontName = "Eras Bold ITC"
			worksheet.Range("A6").Style.Font.Size = 17

			'set the font of Range["A7"]
			worksheet.Range("A7").Text = "Tempus Sans ITC"
			worksheet.Range("A7").Style.Font.FontName = "Tempus Sans ITC"
			worksheet.Range("A7").Style.Font.Size = 17

			'set the font of Range["A8"]
			worksheet.Range("A8").Text = "STENCIL"
			worksheet.Range("A8").Style.Font.FontName = "STENCIL"
			worksheet.Range("A8").Style.Font.Size = 17

			worksheet.AutoFitColumn(1)

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

		End Sub
	End Class
End Namespace