合并EXCEL单元格代码摘记

  /// <summary>
        /// 
        /// </summary>
        /// <param name="fileCopy">目标文件地址</param>
        /// <param name="cellsA">需要合并单元格的开始列名集合,如“A"列,“B"列等,需大写</param>
        /// <param name="cellsB">需要合并单元格的结束列名集合,如“A"列,“B"列等,需大写</param>
        /// <param name="startNum">各列开始合并的行号集合,与cell一一对应</param>
        /// <param name="steps">合并单元格的步数集合,如第一个3,表示从cellsA列名开始 第startNum行开始,到cellB列名结束,合并3行</param>
        /// <param name="SheetName">sheet名称</param>
        /// <param name="TotalCount">每个开始列需要合并多少个单元格的集合</param>

        public static void MergeCells(string fileCopy, string[,] cellsA, string[,] cellsB, int[,] startNum,int[,] steps, int[] TotalCount, string SheetName = "")
        {

            Microsoft.Office.Interop.Excel.Application ThisApplication = new Microsoft.Office.Interop.Excel.Application();
            var ThisWorkbook = ThisApplication.Workbooks.Open(fileCopy, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            ThisApplication.DisplayAlerts = false;
            try
            {
                var xlSheet = string.IsNullOrEmpty(SheetName) ? (Microsoft.Office.Interop.Excel.Worksheet)ThisWorkbook.Worksheets[1] : (Microsoft.Office.Interop.Excel.Worksheet)ThisWorkbook.Worksheets[SheetName];
                Microsoft.Office.Interop.Excel.Range rangeCell;
                int start = 0;

                for(int k=0;k<TotalCount.Length;k++)
                {
                    start = startNum[k, 0];
                    for (int i = 0; i < TotalCount[k]; i++)
                    {
                        string cell1 = cellsA[k, i] + start;
                        string cell2 = cellsB[k, i] + (start + steps[k, i]-1);
                        start=start+ steps[k, i];
                        Microsoft.Office.Interop.Excel.Range excelRange = xlSheet.get_Range(cell1,cell2);

                        excelRange.Merge(excelRange.MergeCells);

                    }

                }
                     
                
                ThisWorkbook.Save();
                ThisWorkbook.Close();
                ThisWorkbook = null;

            }
            catch (Exception ex)
            {
                throw new Exception("动态改变Excel模板列失败");
            }
            finally
            {

                if (ThisWorkbook != null)
                {
                    ThisWorkbook.Close(true, Type.Missing, Type.Missing);

                }
                if (ThisApplication != null)
                {
                    ThisApplication.Quit();
                    KillSpecialExcel(ThisApplication);
                    ThisApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

 


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="cellsA">需要合并的列名集合</param>
        /// <param name="startNum">开始路径</param>
        /// <param name="steps">步数</param>
        /// <param name="TotalCount">总的信息数</param>
        /// <param name="SheetName">表名</param>
        public static void SimpleMergeCells(string filePath, string[] cellsA,  int startNum, int steps, int TotalCount, string SheetName = "")
        {

            Microsoft.Office.Interop.Excel.Application ThisApplication = new Microsoft.Office.Interop.Excel.Application();
            var ThisWorkbook = ThisApplication.Workbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            ThisApplication.DisplayAlerts = false;
            try
            {
                var xlSheet = string.IsNullOrEmpty(SheetName) ? (Microsoft.Office.Interop.Excel.Worksheet)ThisWorkbook.Worksheets[1] : (Microsoft.Office.Interop.Excel.Worksheet)ThisWorkbook.Worksheets[SheetName];
                Microsoft.Office.Interop.Excel.Range rangeCell;
                int start = 0;
                String cell1, cell2;

                for (int k = 0; k < cellsA.Length; k++)
                {
                    start = startNum;
                    for (int i = 0; i <= TotalCount; i++)
                    {
                         cell1 = cellsA[k] + start;
                        if (i==TotalCount)
                        {
                             cell2 = cellsA[k] + (start + steps - 2);
                        }
                        else
                        {
                             cell2 = cellsA[k] + (start + steps - 1);
                        }
                        
                        
                        start = start + steps;
                        Microsoft.Office.Interop.Excel.Range excelRange = xlSheet.get_Range(cell1, cell2);

                        excelRange.Merge(excelRange.MergeCells);

                    }

                }


                ThisWorkbook.Save();
                ThisWorkbook.Close();
                ThisWorkbook = null;

            }
            catch (Exception ex)
            {
                throw new Exception("动态改变Excel模板列失败");
            }
            finally
            {

                if (ThisWorkbook != null)
                {
                    ThisWorkbook.Close(true, Type.Missing, Type.Missing);

                }
                if (ThisApplication != null)
                {
                    ThisApplication.Quit();
                    KillSpecialExcel(ThisApplication);
                    ThisApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

 


        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值