Monday, 19 March 2012 02:55

Easily Merge Excel Cells in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

Excel cells merge is indispensable for handling Excel worksheet. When it comes to the problem of Excel cells merge, developers may encounter different solutions, the solution will be introduced is very simple since users can realize the task of excel cell merge by only one line of code by a .NET Excel component.

Spire.XLS for .NET allows developers to generate, read, write and manipulate Excel files without using MS Excel Automation. Using this .NET component, you can call the method: public void Spire.Xls. Worksheet. Range. Merge() to merge excel cells after loading an Excel file by invoking Workbook. LoadFromFile(string fileName, bool preserveMode) method.

Please preview the effect screenshot after merging the cells as below:

Merge Excel Cells

Here, you can download Spire.XLS for .NET, and follow the whole code below:

Merge Cells in Excel via C#, VB.NET

C# Merge Cells in Excel
using Spire.Xls;

namespace mergecells
{
    class Program
    {
        static void Main(string[] args)
        {
            //load an Excel file from system
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\mergecells.xls", ExcelVersion.Version97to2003);
       
            //merge Excel cells
            Worksheet sheet = workbook.Worksheets[0];
            //merge A3-C5 to one cell
            sheet.Range["A3:C5"].Merge();
            //merge B8-D10 to one cell
            sheet.Range["B8:D10"].Merge();
            //merge C13-E15 to one cell
            sheet.Range["C13:E15"].Merge();

            //save the workbook
            workbook.SaveToFile(@"cellsMerge.xls", ExcelVersion.Version97to2003);
        }
    }
}
VB.NET Merge Cells in Excel
Imports Spire.Xls

Namespace mergecells
	Class Program
		Private Shared Sub Main(args As String())
			'load an Excel file from system
			Dim workbook As New Workbook()
			workbook.LoadFromFile("..\mergecells.xls", ExcelVersion.Version97to2003)

			'merge Excel cells
			Dim sheet As Worksheet = workbook.Worksheets(0)
			'merge A3-C5 to one cell
			sheet.Range("A3:C5").Merge()
			'merge B8-D10 to one cell
			sheet.Range("B8:D10").Merge()
			'merge C13-E15 to one cell
			sheet.Range("C13:E15").Merge()

			'save the workbook
			workbook.SaveToFile("cellsMerge.xls", ExcelVersion.Version97to2003)
		End Sub
	End Class
End Namespace

Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight.

Additional Info

  • tutorial_title: Merge Excel Cells
Last modified on Friday, 11 July 2014 02:51