People always do a lot of special effects for their documents. Text alignment is 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# Excel Text 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:
C# Excel Text Alignment
To set text alignment in Microsoft Excel is very easy. Here I will give another solution, C# Excel Text 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: