如何在EPPlus中水平居中合并的单元格

34

我遇到了一个问题,无法使合并单元格的一段范围水平居中对齐。对齐方式保持为左对齐。这是我的代码。

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个回答

66

应该是:

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

但我认为你应该把它放在最后,因为某些样式更改可能会影响你的对齐方式。顺序很重要。


如何根据条件合并单元格? - Prashant Pimpale

8

居中对齐合并单元格

 // 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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接