Unmerging and merging Excel cells are indispensable for handling Excel worksheet. This article aims at introducing the solution to unmerge Excel cells in c# through several lines of code. We need an Excel .NET component called Spire.XLS to help us complete the process.
First we need to complete the preparatory work before unmerge Excel cells in C#:
- Download the Spire.XLS and install it on your machine.
- Add the Spire.XLS.dll files as reference.
- Open bin folder and select the three dll files under .NET 4.0.
- Right click property and select properties in its menu.
- Set the target framework as .NET 4.
- Add Spire.XLS as namespace.
Here comes to the explanation of the code:
Step 1: Create an instance of Spire.XLS.Workbook.
Workbook book = new Workbook();
Step 2: Load the file base on a specified file path.
book.LoadFromFile(@"..\..\abc.xlsx");
Step 3: Get the first worksheet.
Worksheet sheet = book.Worksheets[0];
Step 4: Unmerge the cells.
sheet.Range["A2"].UnMerge();
Step5: Save as the generated file.
book.SaveToFile(@"..\..\result.xlsx", ExcelVersion.Version2010);
Here is the whole code:
using Spire.Xls; namespace UnmergeExcelCell { class Program { static void Main(string[] args) { Workbook book = new Workbook(); book.LoadFromFile(@"..\..\abc.xlsx"); Worksheet sheet = book.Worksheets[0]; sheet.Range["A2"].UnMerge(); book.SaveToFile(@"..\..\result.xlsx", ExcelVersion.Version2010); } } }
Please preview the original effect screenshot:
And the generated effect screenshot: