合并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();
            }


        }

猜你喜欢

转载自blog.csdn.net/Andrewniu/article/details/89416699