居中、合并和环绕单元格中的文本 - EPPlus

Centering, Merging, and Wrapping Text In Cells - EPPlus

我正在尝试将 header(s) 置于 excel 文件的中心,如下所示:

但是我遗漏了一些细节,因为下面的代码写在一行上并且没有扩展单元格的高度。到目前为止,这是我的代码:

ws.Cells[$"A{row}:F{row}"].Merge = true;
ws.Cells[$"A{row}"].Style.WrapText = true;
ws.SelectedRange[$"A{row}"].Value = purchaseHistory[0].LineText;

要使合并的单元格在垂直和水平方向居中,只需执行以下操作:

//Only need to grab the first cell of the merged range
ws.Cells[$"A{row}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
ws.Cells[$"A{row}"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

如果您需要对行高进行一些操作,您需要查看 CustomHeight 设置。这应该解释一下: