Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Horizontalalign Center merged cells in EPPlus

Tags:

c#

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; 
like image 904
David Choi Avatar asked Dec 08 '14 15:12

David Choi


People also ask

How do you center align text in Excel C#?

HorizontalAlignment = HAlign. Center; . This will align all the cells in your sheet from B1 to B4 (column 2- row 1 through 4).

How do I merge cells in Epplus?

If you want to merge cells dynamically, you can also use: worksheet. Cells[FromRow, FromColumn, ToRow, ToColumn].


2 Answers

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.

like image 94
davidahines Avatar answered Sep 16 '22 22:09

davidahines


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; 
like image 29
Arun Prasad E S Avatar answered Sep 20 '22 22:09

Arun Prasad E S