简体   繁体   中英

EPPlus vertical alignment of text to center of merged cells is not working

I am trying to vertical align the text to the center but its not working. Text is coming at the bottom of merged cell. Here is the minimal code that is not working.

// get handle to the existing worksheet
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("mysheet");

//[row,col]
worksheet.Cells[1, 1].Value = "a1";
worksheet.Cells[2, 1].Value = "a1";
worksheet.Cells[3, 1].Value = "a1";
worksheet.Cells[4, 1].Value = "a1";
worksheet.Cells[5, 1].Value = "a1";
worksheet.Cells[6, 1].Value = "a1";

//comment out below six lines to make it work
worksheet.Cells[1, 2].Value = "11";
worksheet.Cells[2, 2].Value = "12";
worksheet.Cells[3, 2].Value = "13";
worksheet.Cells[4, 2].Value = "14";
worksheet.Cells[5, 2].Value = "15";
worksheet.Cells[6, 2].Value = "16";
//comment out above six lines to make it work

worksheet.Cells["A1:A6"].Merge = true;
worksheet.Cells["A1:A6"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

xlPackage.Save();

Please help.

Adding these lines at the end makes it work:

worksheet.Column(1).Style.VerticalAlignment = ExcelVerticalAlignment.Center; worksheet.Column(2).Style.VerticalAlignment = ExcelVerticalAlignment.Center;

That means I have to center align all the columns in order to center align the first column.

Try this...

using OfficeOpenXml.Style;
worksheet.Cells["A1:A6"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

or ...

worksheet.Cells["A1:A6"]. = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;

Try this...

worksheet.Range["A1", "A6"].VerticalAlignment = XlHAlign.xlHAlignCenter;

Let me know if that works.

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