C# and ExcelLibrary How to Right Align cells

Anthony Abouhassan picture Anthony Abouhassan · Mar 20, 2014 · Viewed 13.4k times · Source

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

Answer

Costas Vrahimis picture Costas Vrahimis · Mar 20, 2014

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