Formatting text in Excel in C#

2014-12-29 08:07:50 Written by  support iceblue
Rate this item
(0 votes)

Spire.XLS can help developers to write rich text into the spreadsheet easily, such as set the text in bold, italic, and set the colour and font for them. We have already shown you how to set Subscript and Superscript in Excel files by using Spire.XLS. This article will focus on demonstrate how to formatting text in Excel sheet in C#.

Spire.Xls offers ExcelFont class and developers can format the text of cells easily. By using RichText, we can add the text into cells and set font for it. Here comes to the steps of how to write rich text into excel cells.

Step 1: Create an excel document and get its first worksheet.

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

Step 2: Create the fonts and sent the format for each font.

//create a font and set the format to bold.
ExcelFont fontBold = workbook.CreateFont();
fontBold.IsBold = true;

//create a font and set the format to underline.              
ExcelFont fontUnderline = workbook.CreateFont();
fontUnderline.Underline = FontUnderlineType.Single;

//create a font and set the format to italic.            
ExcelFont fontItalic = workbook.CreateFont();
fontItalic.IsItalic = true;

//create a font and set the color to green.            
ExcelFont fontColor = workbook.CreateFont();
fontColor.KnownColor = ExcelColors.Green;

Step 3: Set the font for specified cell range.

RichText richText = sheet.Range["A1"].RichText;
richText.Text="It is in Bold";
richText.SetFont(0, richText.Text.Length-1, fontBold);
  
richText = sheet.Range["A2"].RichText;
richText.Text = "It is underlined";
richText.SetFont(0, richText.Text.Length-1, fontUnderline);

richText = sheet.Range["A3"].RichText;
richText.Text = "It is Italic";
richText.SetFont(0, richText.Text.Length - 1, fontItalic);

richText = sheet.Range["A4"].RichText;
richText.Text = "It is Green";
richText.SetFont(0, richText.Text.Length - 1, fontColor);

Step 4: Save the document to file.

workbook.SaveToFile("Sample.xls",ExcelVersion.Version97to2003);

Effective screenshot of writing rich text into excel worksheet:

Formatting text in Excel in C#

Full codes:

using Spire.Xls;
namespace WriteRichtextinExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            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 fontItalic = workbook.CreateFont();
            fontItalic.IsItalic = true;

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

            RichText richText = sheet.Range["A1"].RichText;
            richText.Text = "It is in Bold";
            richText.SetFont(0, richText.Text.Length - 1, fontBold);

            richText = sheet.Range["A2"].RichText;
            richText.Text = "It is underlined";
            richText.SetFont(0, richText.Text.Length - 1, fontUnderline);

            richText = sheet.Range["A3"].RichText;
            richText.Text = "It is Italic";
            richText.SetFont(0, richText.Text.Length - 1, fontItalic);

            richText = sheet.Range["A4"].RichText;
            richText.Text = "It is Green";
            richText.SetFont(0, richText.Text.Length - 1, fontColor);

            workbook.SaveToFile("Sample.xls", ExcelVersion.Version97to2003);

        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Monday, 06 September 2021 02:23