Merge or Unmerge Cells in Excel Worksheet with C#

In this article, we will learn about merging and unmerging of cells in Excel sheet. Generally, merging cells refers to one or more cells joined together to form a large cell. However, the adjacent cells can be merged vertically or horizontally. Whereas, converting a large cell into multiple cells is known as unmerging or splitting cells in Excel worksheet. Let us explore the following use cases:

Merge Cells in Excel Worksheet with C#

You can easily merge cells in excel worksheet with Aspose.Cells for .NET API using C#. In this example, we will be creating a new excel worksheet from scratch and then merge a few cells by following below steps:

  1. Create a Workbook object
  2. Get first worskeet
  3. Merge specific cells
  4. Put value in merged cell
  5. Apply styles on the cell
  6. Save output excel worksheet

Below code snippet shows how to merge cells in Excel worksheet with C#:

The output file generated from this code snippet will contain merged cells as in the screenshot below:

Merge Cells

Unmerge Cells in Excel Worksheet with C#

We have learned how to merge cells in Excel file. Let us take this process another step further. Here we will be considering to unmerge cells in Excel worksheet by following the steps below:

  1. Load source Excel file
  2. Create Worksheet object and access first sheet
  3. Unmerge the cells
  4. Save output file

The code snippet below is based on these steps and shows how to unmerge cells in Excel file with C#:

The following image shows how do the unmerged cells look when you run the above code in your environment:

Unmerge Cells

Merge a Range of Cells in Excel Worksheet with C#

Sometimes there are several cells specified under different ranges of cells. You might want to merge a specific range of cells in Excel sheet. In such scenarios, please follow the steps listed below:

  1. Create a Workbook object
  2. Access the first worksheet
  3. Input data in a Cell
  4. Create and Merge the Range
  5. Save output excel file

The code snippet below shows how to merge range of cells into one larger cell in Excel file using C#:

Unmerge a Range of Cells in Excel Worksheet with C#

We have explored merging a range of cells in Excel in the previous example. Let us move on to unmerging the merged cells by following the steps below:

  1. Load source Workbook
  2. Access specific Worksheet
  3. Create a Range object
  4. Call UnMerge method
  5. Save output Excel file

The code snippet below follows these steps and shows how to unmerge a range of cells in Excel worksheet using C#:

Merge Cells in Named Range in Excel Worksheet with C#

Sometimes the Excel files contain several named ranges because it is easy to identify and manipulate accordingly. Therefore, you can also merge any named range by specifying its name. Resultantly, all the cells in that named range will be merged into one bigger cell. You need to follow these steps:

  1. Load source Excel Workbook
  2. Access the Worksheet containing the named range
  3. Define and Apply style
  4. Merge Named Range
  5. Save output file

The code snippet below explains how to merge cells in a named range in C#:

The output of the above code snippet will be the same as the following image:

Merge Named Range

Conclusion

Merging or unmerging cells in Excel Workbooks and Worksheets is an important and useful feature. We have compiled different possible use cases in this article along with all the steps and images. Now you can easily follow these steps and merge or unmerge cells, ranges, or named ranges in Excel files. In case of any query or concern, you may please contact us via Free Support Forum.

See Also

Import Data from JSON to Excel Worksheet in C# .NET