Home » excel » excel – C# and ExcelLibrary How to Right Align cells

excel – C# and ExcelLibrary How to Right Align cells

Posted by: admin May 14, 2020 Leave a comment


Does anybody know how to make it so that the content passed to the sheet will right align?

How to&Answers:

If you want to align one cell use

worksheet.Cells[y, x].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;

If you want to align more than one

worksheet.get_Range("A1", "A30").Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;

If you can not use Microsoft.Office.Interop.Excel then I am not really sure but you can try to use ExcelCellStyle

ExcelCellStyle titleStyle = workbook.Styles.AddStyle("WorksheetTitleStyle");
// align the text
titleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.right;
titleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;

you might find more here:


using ClosedXML.Excel;

 using (ClosedXML.Excel.XLWorkbook wb = new ClosedXML.Excel.XLWorkbook())
    var ws = wb.Worksheets.Add("WorkSheetName");
    ws.Range(firstCellRow, firstCellColumn, lastCellRow, lastCellColumn).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;

if you want to right allign specific portion in worksheet