EPPlus文本与合并单元格中心的垂直对齐不起作用

时间:2016-07-16 17:56:00

标签: c# asp.net excel epplus

我正在尝试将文本垂直对齐到中心,但它不起作用。文本出现在合并单元格的底部。这是最小的代码无效。

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

请帮忙。

最后添加这些行使其有效:

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

这意味着我必须对齐所有列,以使第一列居中对齐。

2 个答案:

答案 0 :(得分:3)

尝试一下...

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

或...

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

答案 1 :(得分:-3)

试试这个......

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

如果有效,请告诉我。