Introduction

We often format an Excel worksheet to make it be convenient for reading. Usually, we complete Excel formatting through setting cells styles. For example, if the text in one cell is the title of one column or row, we may set its size to be larger than other cells. Also, we may give a color for text in one cell to show that it is the important information. Besides, sometimes, we need to align text in cells. Then, how to set Excel text alignment?

Excel Text Alignment in MS

After we open an Excel worksheet, we can find three buttons about text alignment on tool bar. They are Align Right, Align Center and Align Left. Click the cell where you want to align text and click the buttons to make the text to be the left, center or right.
Additionally, there are other three alignments in Align group, Align Top, Align Middle and Align Bottom. Firstly, select the cell which is needed to set text alignment. Then, right-click and select Set Cell Format. Click the Align tab and then we can choose Align Top, Middle or Bottom.

C#/VB.NET Text Alignment via Spire.XLS

Spire.XLS presents you a convenient way to realize Excel Text Alignment. The same with MS Excel, you should first write text in the worksheet, and then set the text align with the sheet.Range["A1"].Style property. In this property, you may set the horizontal alignment and vertical alignment with sheet.Range["A1"].Style.HorizontalAlignment and sheet.Range["A1"].Style.VerticalAlignment properties, and you may use VerticalAlignType and HorizontalAlignType methods to specify the position of the text. You may use top, bottom, center, left or right to describe the text align. What's more, you may change the direction of the text by assigning the sheet.Range["A1"].Style.Rotation property.
The following code shows us how to set text alignment with C#/VB.NET:
[C#]

using Spire.Xls;

namespace TextAlign
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new workbook
            Workbook workbook = new Workbook();

            //Initialize the worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Vertical top
            sheet.Range["B3"].Text = "Top";
            sheet.Range["B3"].Style.VerticalAlignment = VerticalAlignType.Top;

            //Vertical center
            sheet.Range["B4"].Text = "Center";
            sheet.Range["B4"].Style.VerticalAlignment = VerticalAlignType.Center;

            //Vertical bottom
            sheet.Range["B5"].Text = "Bottom";
            sheet.Range["B5"].Style.VerticalAlignment = VerticalAlignType.Bottom;

            //Horizontal general
            sheet.Range["B6"].Text = "General";
            sheet.Range["B6"].Style.HorizontalAlignment = HorizontalAlignType.General;

            //Horizontal left
            sheet.Range["B7"].Text = "Left";
            sheet.Range["B7"].Style.HorizontalAlignment = HorizontalAlignType.Left;

            //Horizontal center
            sheet.Range["B8"].Text = "Center";
            sheet.Range["B8"].Style.HorizontalAlignment = HorizontalAlignType.Center;

            //Horizontal right
            sheet.Range["B9"].Text = "Right";
            sheet.Range["B9"].Style.HorizontalAlignment = HorizontalAlignType.Right;

            //Rotation 90 degree
            sheet.Range["B10"].Text = "Rotation 90 degree";
            sheet.Range["B10"].Style.Rotation = 90;

            //Rotation 45 degree
            sheet.Range["B11"].Text = "Rotation 45 degree";
            sheet.Range["B11"].Style.Rotation = 45;

            //Save the file
            workbook.SaveToFile("sample.xls");

            //Launch the file
            System.Diagnostics.Process.Start("Sample.xls");
        }
    }
}
          

[Visual Basic]

Imports Spire.Xls

Module Module1

    Sub Main()
        'Create a new workbook
        Dim workbook As New Workbook()

        'Initialize the worksheet
        Dim sheet As Worksheet = workbook.Worksheets(0)

        'Vertical top
        sheet.Range("B3").Text = "Top"
        sheet.Range("B3").Style.VerticalAlignment = VerticalAlignType.Top

        'Vertical center
        sheet.Range("B4").Text = "Center"
        sheet.Range("B4").Style.VerticalAlignment = VerticalAlignType.Center

        'Vertical bottom
        sheet.Range("B5").Text = "Bottom"
        sheet.Range("B5").Style.VerticalAlignment = VerticalAlignType.Bottom

        'Horizontal general
        sheet.Range("B6").Text = "General"
        sheet.Range("B6").Style.HorizontalAlignment = HorizontalAlignType.General

        'Horizontal left
        sheet.Range("B7").Text = "Left"
        sheet.Range("B7").Style.HorizontalAlignment = HorizontalAlignType.Left

        'Horizontal center
        sheet.Range("B8").Text = "Center"
        sheet.Range("B8").Style.HorizontalAlignment = HorizontalAlignType.Center

        'Horizontal right
        sheet.Range("B9").Text = "Right"
        sheet.Range("B9").Style.HorizontalAlignment = HorizontalAlignType.Right

        'Rotation 90 degree
        sheet.Range("B10").Text = "Rotation 90 degree"
        sheet.Range("B10").Style.Rotation = 90

        'Rotation 45 degree
        sheet.Range("B11").Text = "Rotation 45 degree"
        sheet.Range("B11").Style.Rotation = 45

        'Save doc file.
        workbook.SaveToFile("Sample.xls")

        'Launching the MS Word file.
        System.Diagnostics.Process.Start("Sample.xls")
    End Sub
End Module