Merge cells in Excel

  • NPOI
  • Spire.XLS
  • Download Sample Code

using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NOPI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create workbook
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("MySheet");

            //Set the value of the cell
            sheet.CreateRow(0).CreateCell(0).SetCellValue("Spire.XLS");

            //Merge the cell
            CellRangeAddress region = new CellRangeAddress(0, 5, 0, 5);
            sheet.AddMergedRegion(region);

            //Save the file
            FileStream file = File.Create("ExcelMerge.xlsx");
            workbook.Write(file);
            file.Close();

            //Launch the file
            System.Diagnostics.Process.Start("ExcelMerge.xlsx");
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;

namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create workbook
            Workbook workbook = new Workbook();
            
            //Create a new worksheet
            Worksheet sheet = workbook.CreateEmptySheet("MySheet");

            //Set the value of the cell
            sheet.Range["A1"].Text = "Spire.XLS";

            //Merge the cells
            sheet.Range["A1:E5"].Merge();

            //Save and Launch
            workbook.SaveToFile("ExcelMerge.xlsx",ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("ExcelMerge.xlsx");
        }
    }
}