EXCEL Write RichText in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source
 

The sample demonstrates how to write rich text into spreadsheet.

WriteRichText.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];

			ExcelFont fontBold = workbook.CreateFont();
			fontBold.IsBold = true;

			ExcelFont fontUnderline = workbook.CreateFont();
			fontUnderline.Underline = FontUnderlineType.Single;

			ExcelFont fontColor = workbook.CreateFont();
			fontColor.KnownColor  = ExcelColors.Green; 

			RichText richText = sheet.Range["A1"].RichText;
			richText.Text = "Bold and underlined and colored text";
			richText.SetFont(0,3,fontBold);
			richText.SetFont(9,18,fontUnderline);
			richText.SetFont(24,30,fontColor);
			

		
		    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)

			Dim fontBold As ExcelFont = workbook.CreateFont()
			fontBold.IsBold = True

			Dim fontUnderline As ExcelFont = workbook.CreateFont()
			fontUnderline.Underline = FontUnderlineType.Single

			Dim fontColor As ExcelFont = workbook.CreateFont()
			fontColor.KnownColor = ExcelColors.Green

			Dim richText As RichText = sheet.Range("A1").RichText
			richText.Text = "Bold and underlined and colored text"
			richText.SetFont(0,3,fontBold)
			richText.SetFont(9,18,fontUnderline)
			richText.SetFont(24,30,fontColor)



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