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;
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM