Merging and unmerging cells is a simple and commonly used feature of Microsoft Excel. Merging cells might prove to be helpful in scenarios, for example, when you have a worksheet with multiple columns that share the same heading. You can merge the cells above the columns to give them a common heading. In case merged cells are no longer required, you can unmerge them just as easily. You might need to perform these tasks within your C++ applications. For that, this article will teach you how to merge and unmerge cells in Excel worksheets programmatically using C++.

C++ API for Merging and Unmerging Cells

Aspose.Cells for C++ is a native C++ library that allows you to create, read and modify Excel files without requiring Microsoft Excel to be installed. The API also supports merging and unmerging cells in an Excel worksheet. You can either install the API through NuGet or download it directly from the Downloads section.

PM> Install-Package Aspose.Cells.Cpp

Merge Cells in an Excel Worksheet using C++

In this example, we will create an empty Excel worksheet and merge a few cells by following the steps given below.

The following sample code shows how to merge cells in an Excel worksheet using C++.

Image of the output Excel file generated by the sample code

Image of the output Excel file generated by the sample code

Unmerge Cells in an Excel Worksheet using C++

The following are the steps to unmerge cells in an Excel worksheet.

The following sample code demonstrates how to unmerge cells in an Excel worksheet using C++.

Image of the output Excel file generated by the sample code

Image of the output Excel file generated by the sample code

Merge a Range of Cells in an Excel Worksheet using C++

The following are the steps to merge a range of cells in an Excel worksheet.

The following sample code shows how to merge a range of cells in an Excel worksheet using C++.

Image of the output Excel file generated by the sample code

Image of the output Excel file generated by the sample code

Unmerge a Range of Cells in an Excel Worksheet using C++

The following are the steps to unmerge a range of cells in an Excel worksheet.

The following sample code shows how to unmerge a range of cells in an Excel worksheet using C++.

Image of the output Excel file generated by the sample code

Image of the output Excel file generated by the sample code

Merge Cells of a Named Range in an Excel Worksheet using C++

Aspose.Cells for C++ also provides you the ability to merge the cells of a named range. To achieve this, please follow the steps given below.

The following sample code demonstrates how to merge the cells of a named range using C++.

Image of the output Excel file generated by the sample code

Image of the output Excel file generated by the sample code

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to merge and unmerge cells in an Excel worksheet using C++. Furthermore, you have seen how to merge and unmerge ranges and named ranges using Aspose.Cells for C++ API. The API provides many additional features for working with Excel files that you can explore in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.

See Also