title: C# Excel interop merge cells in fast way
body: <p>I want to merge cells and to do it multiple times...
  The reason I'm using this merge its because I want more the one table.
  and a different number of columns for each table and different size of a column on the another...
  The number and the places of the merges are unknown</p>
  
  <pre><code>    using Microsoft.Office.Interop.Excel;
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace ConsoleApp111
  {
      class Program
      {
          static void Main(string[] args)
          {
              string x = @"C:\Users\hed-b\source\repos\TESTUserFormFromExcel\ConsoleApp111\bin\Debug\TEST.xltm";
  
              Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application
              {
                  DisplayAlerts = false,
                  Visible = false,
                  ScreenUpdating = false,
              };
  
              Workbook wb = xlApp.Workbooks.Open(x);
              Worksheet ws = (Worksheet)wb.Worksheets[1];
              if (ws == null)
              {
                  Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
                  return;
              }
              Stopwatch stopWatch = Stopwatch.StartNew();
  
              for (int i = 2; i &lt;= 10; i++)
              {
                  ws.get_Range("A"+i, "B"+i).Merge();
  
              }
              for (int i = 2; i &lt;= 10; i++)
              {
                  ws.get_Range("C" + i, "D" + i).Merge();
              }
              for (int i = 2; i &lt;= 10; i++)
              {
                  ws.get_Range("E" + i, "H" + i).Merge();
              }
              Console.WriteLine(stopWatch.ElapsedMilliseconds);
  
              stopWatch = Stopwatch.StartNew();
              ws.get_Range("A" + 2, "H" + 10).Value2 = new string[10, 5]{
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                  { "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
              };
  
              ws.get_Range("A" + 11, "H" + 15).Value2 = new string[5, 8]{
                  { "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"},
                  { "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"},
                  { "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"},
                  { "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"},
                  { "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"}
              };
  
              Console.WriteLine(stopWatch.ElapsedMilliseconds);
  
              xlApp.ScreenUpdating = true;
              xlApp.Visible = true;
          }
      }
  }
  </code></pre>
  
  <p>I just looking for better way to do it faster </p>
  
  <p>All these example program merges take about 1724 ms. in my actual program I have much more merges and take more than 5000 ms. 
  This merges its what take the most of the time...</p>