This simple tutorial will provide detail on how to merge cells in Excel using C#. With the help of C# Excel worksheet merge cells operation can be performed using a few simple API calls. Moreover, you do not need to install MS Excel for merging the cells and creating the output file in XLSX or XLS format.
Steps to Merge Cells in Excel using C#
- Add the reference to Aspose.Cells in your merge cells application with the NuGet Package Manager
- Initialize a Workbook class object to create a new Excel file
- Access a worksheet and create a Cells class object
- Merge the cells and enter a sample value in the merged cell
- Save output Excel file with merged cells
These concise steps elaborate how using C# Excel merge cells feature can be incorporated in your .NET applications. First you need to create a worksheet object and then merge particular cells by specifying the column and row indexes and finally, save the output Excel workbook which will contain a sample value and merged cells as per your requirements.
Code to Merge Cells in Excel using C#
Here to merge cells in Excel C# sample code is used that called Cells.Merge() function to create a range of merged cells using first row, first column, total rows and total columns values as input. If you want to merge and center cells in Excel using C#, you may simply get the style of the cell where you put value and set HorizontalAlignment property of the style object to TextAlignmentType.Center. Now after saving the workbook, the sample value will be displayed at the center of the merged cells.
Here we have observed the process to merge cells in a workbook using C#. However, if you are interested to learn Excel to HTML conversion then refer to the article on how to convert Excel to HTML in C#.