Set Text Direction in Excel Workbook in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source


The sample demonstrates how to design text direction in excel spreadsheet.



using Spire.Xls;

namespace TextDirection
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\..\..\Data\Blank.xls",ExcelVersion.Version97to2003);
            Worksheet worksheet = workbook.Worksheets[0];

            //rotate 90 degrees of Range["A1"] text
            worksheet.Range["A1"].Text = "Rotate 30 degrees";
            worksheet.Range["A1"].Style.Rotation = 90;

            //rotate 30 degrees of Range["A2"] text
            worksheet.Range["A2"].Text = "Rotate 45 degrees";
            worksheet.Range["A2"].Style.Rotation = 30;

            //rotate 45 degrees of Range["B1"] text
            worksheet.Range["B1"].Text = "Rotate 90 degrees";
            worksheet.Range["B1"].Style.Rotation = 45;

            //rotate 180 degrees of Range["B2"] text
            worksheet.Range["B2"].Text = "Rotate 180 degrees";
            worksheet.Range["B2"].Style.Rotation = 180;

            worksheet.AutoFitColumn(1);
            worksheet.AutoFitColumn(2);
        
            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Imports Spire.Xls

Namespace TextDirection
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim workbook As New Workbook()
			workbook.LoadFromFile("..\..\..\..\Data\Blank.xls",ExcelVersion.Version97to2003)
			Dim worksheet As Worksheet = workbook.Worksheets(0)

			'rotate 90 degrees of Range["A1"] text
			worksheet.Range("A1").Text = "Rotate 30 degrees"
			worksheet.Range("A1").Style.Rotation = 90

			'rotate 30 degrees of Range["A2"] text
			worksheet.Range("A2").Text = "Rotate 45 degrees"
			worksheet.Range("A2").Style.Rotation = 30

			'rotate 45 degrees of Range["B1"] text
			worksheet.Range("B1").Text = "Rotate 90 degrees"
			worksheet.Range("B1").Style.Rotation = 45

			'rotate 180 degrees of Range["B2"] text
			worksheet.Range("B2").Text = "Rotate 180 degrees"
			worksheet.Range("B2").Style.Rotation = 180

			worksheet.AutoFitColumn(1)
			worksheet.AutoFitColumn(2)

			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
	End Class
End Namespace