EPPlus 合并单元格

/// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="col">要合并的列索引</param>
        private void MergeCell(ExcelWorksheet sheet, int col)
        {
            int fromRow = ROWSTARTINDEX + 1;
            int fromCol = col;
            int toRow = ROWSTARTINDEX + 1;
            int toCol = col;

            string v = sheet.Cells[ROWSTARTINDEX + 1, col].Value.ToString();

            for (int i = ROWSTARTINDEX + 1; i < sheet.Cells.Rows; i++)
            {
                var vNext = sheet.Cells[i + 1, col].Value?.ToString();

                if (v != vNext)
                {
                    v = vNext;
                    toRow = i;
                    sheet.Cells[fromRow, fromCol, toRow, toCol].Merge = true;
                    sheet.Cells[fromRow, fromCol, toRow, toCol].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    fromRow = i + 1;
                }

                if (vNext == null)
                {
                    break;
                }
            }
        }

 

posted @ 2020-05-22 14:00  も不秃不秃  阅读(3747)  评论(0编辑  收藏  举报