Does anybody know how to make it so that the content passed to the sheet will right align?
Asked
Active
Viewed 1.5k times
2 Answers
2
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

Costas Vrahimis
- 519
- 1
- 8
- 17
-
Is this with the ExcelLibrary.Spreadsheet or with Microsoft.Office.Interop.Excel? The machine this is running on doesn't have excel installed on it so i cant use Interop – Anthony Abouhassan Mar 20 '14 at 15:19
-
If you can not use Microsoft.Office.Interop.Excel then I am not 100% on this but you might be able to use what I added about the ExcelCellStyle. also change workbook to whatever you named your instance of workbook. Hope this helps – Costas Vrahimis Mar 20 '14 at 15:45
-
It doesn't have that property, but thank you for the help anyway! – Anthony Abouhassan Mar 21 '14 at 14:11
0
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

Aikansh Mann
- 606
- 6
- 12