Excel Cell Text alignment
- EPPlus supports main two categories of (Horizontal & Vertical) text alignments.
- Each alignment is respectively assigned by ExcelHorizontalAlignment& ExcelVerticalAlignment enum.
*By default, EPPlus supports Left Horizontal & Bottom Vertical text alignment, if you are not specifying any alignment.
Excel Row Height
*In this example, Row method accepts value 4 as a parameter for row number & height properties assigns 30 as double type.
Column Width
*In this example, dimension property is the address of the worksheet from left to right cell & AutoFitColumns() is responsible to set columns width, as per the length of content of the cell range.
Output on an Excel sheet is given below.
Source code
- using OfficeOpenXml;
- using System.IO;
- using System;
-
- using OfficeOpenXml.Style;
- using System.Drawing;
- class Program {
- static void Main(string[] args) {
- ExcelPackage ExcelPkg = new ExcelPackage();
- ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");
- using(ExcelRange Rng = wsSheet1.Cells[2, 2, 2, 2]) {
- Rng.Value = "Welcome to Everyday be coding - tutorials for beginners";
- Rng.Style.Font.Size = 16;
- Rng.Style.Font.Bold = true;
- Rng.Style.Font.Italic = true;
- }
- wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns();
- using(ExcelRange Rng = wsSheet1.Cells[4, 2, 4, 2]) {
- wsSheet1.Row(4).Height = 30;
- Rng.Value = "Horizontal: CENTER & Vertical: CENTER";
- Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
- Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
- }
- using(ExcelRange Rng = wsSheet1.Cells[5, 2, 5, 2]) {
- wsSheet1.Row(5).Height = 30;
- Rng.Value = "Horizontal: LEFT & Vertical: TOP";
- Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
- Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Top;
- }
- using(ExcelRange Rng = wsSheet1.Cells[6, 2, 6, 2]) {
- wsSheet1.Row(6).Height = 30;
- Rng.Value = "Horizontal: RIGHT & Vertical: BOTTOM";
- Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
- Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom;
- }
- using(ExcelRange Rng = wsSheet1.Cells[7, 2, 7, 2]) {
- wsSheet1.Row(7).Height = 30;
- Rng.Value = "Horizontal: FILL & Vertical: DISTRIBUTED";
- Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Fill;
- Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Distributed;
- }
- wsSheet1.Protection.IsProtected = false;
- wsSheet1.Protection.AllowSelectLockedCells = false;
- ExcelPkg.SaveAs(new FileInfo(@ "D:\New.xlsx"));
- }
- }
Now, build & execute the code. File is (New.xlsx), which is stored on D: drive of the computer.
Thank you for reading this blog.