Forums

Overview » C# » How to Set Text Alignment in Excel with C#
Reply

How to Set Text Alignment in Excel with C#

Yaths Ho
Member



Since: 08 Apr 2012
Posts: 87
Posted 01 Jun 2012 02:58:59

In order to have a good appearance, people often set format for Excel cells. Following, I want to share a method about how to set one kind of cell format, text alignment in Excel with C#. And a component, Spire.XLS is used for realizing this function.

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

namespace ExcelTextAlignment
{
    class Alignment
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            sheet.Range["B1"].Text = "Text Align";
            sheet.Range["B1"].Style.Font.IsBold = true;

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

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

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

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

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

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

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

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

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

            sheet.AllocatedRange.AutoFitColumns();
            sheet.Range["B3:B5"].RowHeight = 20;

            workbook.SaveToFile("Alignment.xlsx");
            System.Diagnostics.Process.Start("Alignment.xlsx");   
        }
    }
}

In order to have a good appearance, people often set format for Excel cells. Following, I want to share a method about how to set one kind of cell format, text alignment in Excel with C#. And a component, Spire.XLS is used for realizing this function.

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

namespace ExcelTextAlignment
{
    class Alignment
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            sheet.Range["B1"].Text = "Text Align";
            sheet.Range["B1"].Style.Font.IsBold = true;

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

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

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

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

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

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

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

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

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

            sheet.AllocatedRange.AutoFitColumns();
            sheet.Range["B3:B5"].RowHeight = 20;

            workbook.SaveToFile("Alignment.xlsx");
            System.Diagnostics.Process.Start("Alignment.xlsx");   
        }
    }
}

Reply to this topic

Message
Reply
Follow us on twitter Subscribe to our RSS feed
Activate your free membership today | Login | Currency