ComponentOne Excel for .NET
In This Topic
    Merging Cells
    In This Topic

    Cells can be merged using the C1Excel.MergedCells property and providing the range of cells to be merged together. Merged cells can be used for inspecting, adding, or clearing merged ranges in a sheet. Each merged range is represented by the C1.C1Excel.XLCellRange object. Cell merges are preserved when adding and removing rows or columns.

    To merge cells, complete the following steps:

    1. Double-click the C1XLBook component in the Toolbox to add it to your form.
    2. Use the following code to create two XLCellRanges (_ColRange and _RowRange) and add them to the MergedCells collection. The code will also apply styles to the merged cells. You can add this code to the Form1_Load event, for example:

      To write code in C#

      C#
      Copy Code
      private void Form1_Load(object sender, EventArgs e)
      {
              //Select range to Merge
              XLCellRange _ColRange = new C1.C1Excel.XLCellRange(4, 6, 0, 8);
              XLCellRange _RowRange = new C1.C1Excel.XLCellRange(9, 21, 3, 4);
               //Set Text for Merged Cells
              c1XLBook1.Sheets[0][4, 0].Value = "Merged Cells";
              c1XLBook1.Sheets[0][9, 3].Value = "Merged Cells";
              //Merge Cells
              c1XLBook1.Sheets[0].MergedCells.Add(_ColRange);
              c1XLBook1.Sheets[0].MergedCells.Add(_RowRange);
              //Define and Apply Styles for Merged Cells
              XLStyle _Colstyle = new C1.C1Excel.XLStyle(c1XLBook1);
              _Colstyle.BackColor = Color.Yellow;
              XLStyle _Rowstyle = new XLStyle(c1XLBook1);
              _Rowstyle.BackColor = Color.LightBlue;
              c1XLBook1.Sheets[0][4, 0].Style = _Colstyle;
              c1XLBook1.Sheets[0][9, 3].Style = _Rowstyle;
              //Save and open the book
              c1XLBook1.Save(@"c:\mybook.xls");
              System.Diagnostics.Process.Start(@"C:\mybook.xls");
      }