How to set the font and background for TextBox in Excel Chart

2014-09-10 01:44:39 Written by  support iceblue
Rate this item
(0 votes)

Textbox is been widely used in Excel workbooks to give abstract and introduce information for a part of the excel documents. Spire.XLS supports to insert textbox in Excel worksheet and edit the setting of the textbox. We have already shown you how to remove the borderline of textbox in Excel chart. This tutorial will demonstrate how to set the font and background for TextBox in Excel in C#.

Firstly, make sure that Spire.XLS for .NET has been installed on your machine. And then, adds Spire.XLS.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.XLS\Bin\NET4.0\ Spire.XLS.dll".

Now it comes to the details of how to set the text font and background color for textbox in Excel in C# and view the textbox before editing:

How to set the font and background for TextBox in Excel Chart

Step 1: Create a new instance of workbook and load an Excel file with textbox from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx");

Step 2: Get the worksheet named in "Product Report" which contains textbox.

Worksheet sheet = workbook.Worksheets["Product Report"];

Step 3: Get the second textbox which will be edited.

XlsTextBoxShape shape = sheet.TextBoxes[1] as XlsTextBoxShape;

Step 4: Set the font and background color for the second textbox.

//Set the font
ExcelFont font = workbook.CreateFont();
font.FontName = "Century Gothic";
font.Size = 10;
font.IsBold = true;
font.Color = Color.Blue;
(new RichText(shape.RichText)).SetFont(0, shape.Text.Length - 1, font);
//set background color
shape.Fill.FillType = ShapeFillType.SolidColor;
shape.Fill.ForeKnownColor = ExcelColors.BlueGray;

Step 5: Save the document to file and launch it.

string output = "result.xlsx";
workbook.SaveToFile(output,ExcelVersion.Version2010);
System.Diagnostics.Process.Start(output);

Effective screenshot after set the font and background color for textbox in Excel chart:

How to set the font and background for TextBox in Excel Chart

Full codes:

using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Shapes;
using System.Drawing;
namespace setFontandBackgroundforTextbox
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("sample.xlsx");
            Worksheet sheet = workbook.Worksheets["Product Report"];
            XlsTextBoxShape shape = sheet.TextBoxes[1] as XlsTextBoxShape;

            //set font
            ExcelFont font = workbook.CreateFont();
            //font.IsStrikethrough = true;
            font.FontName = "Century Gothic";
            font.Size = 10;
            font.IsBold = true;
            font.Color = Color.Blue;
            (new RichText(shape.RichText)).SetFont(0, shape.Text.Length - 1, font);
            //set background color
            shape.Fill.FillType = ShapeFillType.SolidColor;
            shape.Fill.ForeKnownColor = ExcelColors.BlueGray;

            string output = "result.xlsx";
            workbook.SaveToFile(output,ExcelVersion.Version2010);
            System.Diagnostics.Process.Start(output);

        }
    }

Additional Info

  • tutorial_title: Set the font and background for TextBox
Last modified on Monday, 06 September 2021 02:32