Anthony Abouhassan
Anthony Abouhassan

Reputation: 65

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?

Upvotes: 2

Views: 15806

Answers (2)

Aikansh Mann
Aikansh Mann

Reputation: 666

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

Upvotes: 0

Costas Vrahimis
Costas Vrahimis

Reputation: 539

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

Upvotes: 3

Related Questions