EXCEL Font Styles in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source
 

The sample demonstrates how to set font formatting in an excel workbook.

FontStyles.gif

		private void ExcelDocViewer( string fileName )
		{
			try
			{
				System.Diagnostics.Process.Start(fileName);
			}
			catch{}
		}

		private void btnRun_Click(object sender, System.EventArgs e)
		{
			Workbook workbook = new Workbook();
			Worksheet sheet = workbook.Worksheets[0];

			sheet.Range["B1"].Text = "Font setting";
                        sheet.Range["B1"].Style.Font.IsBold = true;

			sheet.Range["B3"].Text = "Arial";
			sheet.Range["B3"].Style.Font.FontName = "Arial";

			sheet.Range["B4"].Text = "Large size";
			sheet.Range["B4"].Style.Font.Size = 20;

			sheet.Range["B5"].Text = "Bold";
			sheet.Range["B5"].Style.Font.IsBold = true;

			sheet.Range["B6"].Text = "Italic";
			sheet.Range["B6"].Style.Font.IsItalic = true;

			sheet.Range["B7"].Text = "Superscript";
			sheet.Range["B7"].Style.Font.IsSuperscript = true;

			sheet.Range["B8"].Text = "Colored";
			sheet.Range["B8"].Style.Font.Color = Color.FromArgb(255,125,125);

			sheet.Range["B9"].Text = "Underline";
			sheet.Range["B9"].Style.Font.Underline = FontUnderlineType.Single;

			sheet.AutoFitColumn(2);
			
		
			workbook.SaveToFile("Sample.xls");
			ExcelDocViewer(workbook.FileName);
		}

		Private Sub ExcelDocViewer(ByVal fileName As String)
			Try
				System.Diagnostics.Process.Start(fileName)
			Catch
			End Try
		End Sub

		Private Sub btnRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click
			Dim workbook As Workbook = New Workbook()
			Dim sheet As Worksheet = workbook.Worksheets(0)

			sheet.Range("B1").Text = "Font setting"
			sheet.Range("B1").Style.Font.IsBold = True

			sheet.Range("B3").Text = "Arial"
			sheet.Range("B3").Style.Font.FontName = "Arial"

			sheet.Range("B4").Text = "Large size"
			sheet.Range("B4").Style.Font.Size = 20

			sheet.Range("B5").Text = "Bold"
			sheet.Range("B5").Style.Font.IsBold = True

			sheet.Range("B6").Text = "Italic"
			sheet.Range("B6").Style.Font.IsItalic = True

			sheet.Range("B7").Text = "Superscript"
			sheet.Range("B7").Style.Font.IsSuperscript = True

			sheet.Range("B8").Text = "Colored"
			sheet.Range("B8").Style.Font.Color = Color.FromArgb(255,125,125)

			sheet.Range("B9").Text = "Underline"
			sheet.Range("B9").Style.Font.Underline = FontUnderlineType.Single

			sheet.AutoFitColumn(2)


			workbook.SaveToFile("Sample.xls")
			ExcelDocViewer(workbook.FileName)
		End Sub