Search code examples
c#excelexcellibrary

C# and ExcelLibrary How to Right Align cells


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


Solution

  • 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: http://www.winnovative-software.com/ExcelLibDemo/RangesAndCells.aspx