In this article, you will learn about how to merge or unmerge Excel cells in a worksheet in C#. 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 Excel Cells in Worksheet with C#
- Unmerge Excel Cells in Worksheet with C#
- Merge a Range of Excel Cells in Worksheet with C#
- Unmerge a Range of Excel Cells in Worksheet with C#
- Merge Excel Cells of Named Range in Worksheet with C#
Merge Excel Cells in 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:
- Create a Workbook object
- Get first worskeet
- Merge specific cells
- Put value in merged cell
- Apply styles on the cell
- 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:
Unmerge Excel Cells in 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:
- Load source Excel file
- Create Worksheet object and access first sheet
- Unmerge the cells
- 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:
Merge a Range of Excel Cells in 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:
- Create a Workbook object
- Access the first worksheet
- Input data in a Cell
- Create and Merge the Range
- 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 Excel Cells in 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:
- Load source Workbook
- Access specific Worksheet
- Create a Range object
- Call UnMerge method
- 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 Excel Cells of Named Range in 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:
- Load source Excel Workbook
- Access the Worksheet containing the named range
- Define and Apply style
- Merge Named Range
- 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:
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.