简体   繁体   中英

How to Horizontalalign Center merged cells in EPPlus

I am having an issue getting a range of merged cells to horizontal align centered. The alignment stays as left. Here's my code.

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;

Should be:

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

But I think you should be doing it last, as some styling changes can affect your alignment. The order matters.

Center align merged cells

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM