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;
HorizontalAlignment = HAlign. Center; . This will align all the cells in your sheet from B1 to B4 (column 2- row 1 through 4).
If you want to merge cells dynamically, you can also use: worksheet. Cells[FromRow, FromColumn, ToRow, ToColumn].
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With