How to Set Text Directions in Excel Cell with C#

As is known, data can be text or number. After exporting data to Excel, we need to format it to have a good layout and appearance. Font style plays an important role in cell formats. In this post, I will talk something about text direction, one part of font style settings.

Generally speaking, the first row or column is often used to save title. These titles may have some special formats, for example, vertical forms. Therefore, in order to achieve it, we should change text direction in cell from default format.

In my example, I will create an empty workbook and enter some texts in cells. Then set different directions for them, including 30, 45, 90, 180 degrees rotation.

Step 1: Create new workbook and initialize worksheet.

            //Initialize Worksheet

            Workbook workbook = new Workbook();

            workbook.CreateEmptySheets(1);

            Worksheet worksheet = workbook.Worksheets[0];

Step 2: Enter text in cells and set direction.

            worksheet.Range[“A1”].Text = “Rotate 90 degrees”;

            worksheet.Range[“A1”].Style.Rotation = 90;

            worksheet.Range[“A2”].Text = “Rotate 30 degrees”;

            worksheet.Range[“A2”].Style.Rotation = 30;

            worksheet.Range[“B1”].Text = “Rotate 45 degrees”;

            worksheet.Range[“B1”].Style.Rotation = 45;

            worksheet.Range[“B2”].Text = “Rotate 180 degrees”;

            worksheet.Range[“B2”].Style.Rotation = 180;

Step 3: Format font style from A1 to B2, including font type name, font color and font size.

            worksheet.Range[“A1:B2”].Style.Font.FontName = “Calibri”;

            worksheet.Range[“A1:B2”].Style.Font.Color = Color.DarkCyan;

            worksheet.Range[“A1:B2”].Style.Font.Size = 12;

            worksheet.Range[“A1:B2”].Style.Font.IsBold = true;

Step 4: Auto fit column width and save this Excel file.

            worksheet.AutoFitColumn(1);

            worksheet.AutoFitColumn(2);

            workbook.SaveToFile(“Direction.xlsx”, ExcelVersion.Version2010);

            System.Diagnostics.Process.Start(workbook.FileName);

Note:  this method is based on a component: Spire.XLS. So I add its DLL file as reference before coding.

Coding:

using System;
using System.Drawing;
using Spire.Xls;

namespace ExcelTextDirection
{
    class Direction
    {
        static void Main(string[] args)
        {

            //Initialize Worksheet
            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet worksheet = workbook.Worksheets[0];

            //Set Text Direction in Cells
            worksheet.Range[“A1”].Text = “Rotate 90 degrees”;
            worksheet.Range[“A1”].Style.Rotation = 90;
            worksheet.Range[“A2”].Text = “Rotate 30 degrees”;
            worksheet.Range[“A2”].Style.Rotation = 30;
            worksheet.Range[“B1”].Text = “Rotate 45 degrees”;
            worksheet.Range[“B1”].Style.Rotation = 45;
            worksheet.Range[“B2”].Text = “Rotate 180 degrees”;
            worksheet.Range[“B2”].Style.Rotation = 180;

            //Set Font Style
            worksheet.Range[“A1:B2”].Style.Font.FontName = “Calibri”;
            worksheet.Range[“A1:B2”].Style.Font.Color = Color.DarkCyan;
            worksheet.Range[“A1:B2”].Style.Font.Size = 12;
            worksheet.Range[“A1:B2”].Style.Font.IsBold = true;

            //Set Column Width
            worksheet.AutoFitColumn(1);
            worksheet.AutoFitColumn(2);

            //Save and Launch File
            workbook.SaveToFile(“Direction.xlsx”, ExcelVersion.Version2010);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Result:

__________________________________________________________________________

Click Here to LEARN MORE about Spire.XLS

Click Here to DOWNLOAD Spire.xLS

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

2 thoughts on “How to Set Text Directions in Excel Cell with C#”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s