C# Excel – Set Text Alignment

People always do a lot of special effects for their documents. Text alignment should be one of them. Although text alignment is not as useful in Excel as it is in Word. We still have a lot of choices for our Excel text alignment such as left align, right align, center, justified, top, bottom, rotation, etc. The most useful for text alignment in Excel may be centering text.

C# Excel Text Alignment

To set text alignment in Microsoft Excel is very easy. Here I will give another solution, C# ExcelText Alignment. We will use C# code to set text alignment in Excel file. Use C# set Excel text alignment can be easy through Spire.XLS for .NET which is a .NET Excel component enables your .NET applications fast generate, read, write and modify Excel document without Microsoft Office Excel Automation.

Download Spire.XLS (or Spire.Office which includes Spire.XLS) with .NET Framework 2.0 (or above) together. Spire.XLS presents an easy way for you to set text alignment including top, bottom, center, right, left, general, rotation, etc. Use the code below in your application and you can easily C# excel text alignment.

Set Text Alignment in Excel with C# Code:

01    private void ExcelDocViewer( string fileName )
02    {
03        try
04        {
05            System.Diagnostics.Process.Start(fileName);
06        }
07        catch{}
08    }
09   
10    private void btnRun_Click(object sender, System.EventArgs e)
11    {
12        Workbook workbook = new Workbook();
13        Worksheet sheet = workbook.Worksheets[0];
14   
15        sheet.Range["B1"].Text = "Text Align";
16                    sheet.Range["B1"].Style.Font.IsBold = true;
17   
18        sheet.Range["B3"].Text = "Top";
19        sheet.Range["B3"].Style.VerticalAlignment = VerticalAlignType.Top;
20   
21        sheet.Range["B4"].Text = "Center";
22        sheet.Range["B4"].Style.VerticalAlignment = VerticalAlignType.Center;
23   
24        sheet.Range["B5"].Text = "Bottom";
25        sheet.Range["B5"].Style.VerticalAlignment = VerticalAlignType.Bottom;
26   
27        sheet.Range["B6"].Text = "General";
28        sheet.Range["B6"].Style.HorizontalAlignment = HorizontalAlignType.General;
29   
30        sheet.Range["B7"].Text = "Left";
31        sheet.Range["B7"].Style.HorizontalAlignment = HorizontalAlignType.Left;
32   
33        sheet.Range["B8"].Text = "Center";
34        sheet.Range["B8"].Style.HorizontalAlignment = HorizontalAlignType.Center;
35   
36        sheet.Range["B9"].Text = "Right";
37        sheet.Range["B9"].Style.HorizontalAlignment = HorizontalAlignType.Right;
38   
39        sheet.Range["B10"].Text = "Rotation 90 degree";
40        sheet.Range["B10"].Style.Rotation = 90;
41   
42        sheet.Range["B11"].Text = "Rotation 45 degree";
43        sheet.Range["B11"].Style.Rotation = 45;
44   
45   
46        sheet.AllocatedRange.AutoFitColumns();
47        sheet.Range["B3:B5"].RowHeight = 20;
48   
49        workbook.SaveToFile("Sample.xls");
50        ExcelDocViewer(workbook.FileName);
51    }

After running your application with the code above, your text alignment in excel would be finished setting as the image below:

PS:
VB.NET Code:

01	Private Sub ExcelDocViewer(ByVal fileName As String)
02	    Try
03	        System.Diagnostics.Process.Start(fileName)
04	    Catch
05	    End Try
06	End Sub
07
08	Private Sub btnRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click
09	    Dim workbook As Workbook = New Workbook()
10	    Dim sheet As Worksheet = workbook.Worksheets(0)
11
12	    sheet.Range("B1").Text = "Text Align"
13	    sheet.Range("B1").Style.Font.IsBold = True
14
15	    sheet.Range("B3").Text = "Top"
16	    sheet.Range("B3").Style.VerticalAlignment = VerticalAlignType.Top
17
18	    sheet.Range("B4").Text = "Center"
19	    sheet.Range("B4").Style.VerticalAlignment = VerticalAlignType.Center
20
21	    sheet.Range("B5").Text = "Bottom"
22	    sheet.Range("B5").Style.VerticalAlignment = VerticalAlignType.Bottom
23
24	    sheet.Range("B6").Text = "General"
25	    sheet.Range("B6").Style.HorizontalAlignment = HorizontalAlignType.General
26
27	    sheet.Range("B7").Text = "Left"
28	    sheet.Range("B7").Style.HorizontalAlignment = HorizontalAlignType.Left
29
30	    sheet.Range("B8").Text = "Center"
31	    sheet.Range("B8").Style.HorizontalAlignment = HorizontalAlignType.Center
32
33	    sheet.Range("B9").Text = "Right"
34	    sheet.Range("B9").Style.HorizontalAlignment = HorizontalAlignType.Right
35
36	    sheet.Range("B10").Text = "Rotation 90 degree"
37	    sheet.Range("B10").Style.Rotation = 90
38
39	    sheet.Range("B11").Text = "Rotation 45 degree"
40	    sheet.Range("B11").Style.Rotation = 45
41
42
43	    sheet.AllocatedRange.AutoFitColumns()
44	    sheet.Range("B3:B5").RowHeight = 20
45
46	    workbook.SaveToFile("Sample.xls")
47	    ExcelDocViewer(workbook.FileName)
48	End Sub

Leave a Reply