C# 使用Epplus导出Excel [4]:合并指定行
C# 使用Epplus导出Excel [1]:导出固定列数据
C# 使用Epplus导出Excel [2]:导出动态列数据
C# 使用Epplus导出Excel [3]:合并列连续相同数据
导出的Excel数据,合并指令行并水平垂直居中
原始数据
合并后的数据
代码如下
/// <summary> /// 合并行 /// </summary> /// <param name="sheet"></param> /// <param name="startRowIndex"></param> /// <param name="mergeRowIndexs">合并行的行数,起始位置,终止位置</param> public static void MergeRowCells(ExcelWorksheet sheet, int startRowIndex, int[,] mergeRowIndexs) { for (int i = 0; i < mergeRowIndexs.Rank; i++) { sheet.Cells[mergeRowIndexs[i, 0] + startRowIndex, mergeRowIndexs[i, 1], mergeRowIndexs[i, 0] + startRowIndex, mergeRowIndexs[i, 2]].Merge = true; sheet.Cells[mergeRowIndexs[i, 0] + startRowIndex, mergeRowIndexs[i, 1], mergeRowIndexs[i, 0] + startRowIndex, mergeRowIndexs[i, 2]].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center; sheet.Cells[mergeRowIndexs[i, 0] + startRowIndex, mergeRowIndexs[i, 1], mergeRowIndexs[i, 0] + startRowIndex, mergeRowIndexs[i, 2]].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center; } }
//行 int[,] mergeRowIndexs= { { 6, 2, 3 }, { 7, 2, 3 } }; EpplusHelper.MergeRowCells(worksheet, 2,mergeRowIndexs); package.Save();