C#和ExcelLibrary如何正确对齐单元格

时间:2014-03-20 14:19:29

标签: c# excel excellibrary

是否有人知道如何制作它以使传递给工作表的内容正确对齐?

2 个答案:

答案 0 :(得分:3)

如果要对齐一个单元格,请使用

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

如果您想对齐多个

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

如果您无法使用Microsoft.Office.Interop.Excel,那么我不太确定,但您可以尝试使用ExcelCellStyle

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

你可能会在这里找到更多: http://www.winnovative-software.com/ExcelLibDemo/RangesAndCells.aspx

答案 1 :(得分: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;
  }

如果您想纠正工作表中的Allign特定部分