如何在EPPlus中合并Horizo​​ntalalign中心的单元格

时间:2014-12-08 15:03:05

标签: c# epplus

我遇到一个问题,即将一系列合并的单元格设置为水平对齐居中。对齐保持为左侧。这是我的代码。

ws.Cells[lStartColumn + lStartRow].Value = gPortfolioName + " - " + lTypeOfPortfolioPerf + " Performance Update";
ws.Cells[lStartColumn + lStartRow + ":" + lEndColumn + lEndRow].Merge = true;
ws.Cells[lStartColumn + lStartRow + ":" + lEndColumn + lEndRow].Style.HorizontalAlignment = ExcelHorizontalAlignment.CenterContinuous;
ws.Cells[lStartColumn + lStartRow + ":" + lEndColumn + lEndRow].Style.Font.Size = 14;
ws.Cells[lStartColumn + lStartRow + ":" + lEndColumn + lEndRow].Style.Font.Color.SetColor(bgTitleColor);
ws.Cells[lStartColumn + lStartRow + ":" + lEndColumn + lEndRow].Style.Font.Bold = true;

2 个答案:

答案 0 :(得分:45)

应该是:

worksheet.Cells["A2:A4"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

但我认为你最后应该这样做,因为一些样式更改会影响你的对齐。订单很重要。

答案 1 :(得分:5)

居中对齐合并的单元格

 // ws.Cells[Rowstart, ColStart, RowEnd, ColEnd]

  ws.Cells[1, 1].Value = "BILL OF MATERIALS";
  ws.Cells[1, 1, 1, 7].Merge = true; //Merge columns start and end range
  ws.Cells[1, 1, 1, 7].Style.Font.Bold = true; //Font should be bold
  ws.Cells[1, 1, 1, 7].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // Alignment is center
  ws.Cells[1, 1, 1, 7].Style.Font.Size = 25;