C # Get Excel Merge cells in
When we make a table, it is sometimes necessary to merge and ungroup some cells. It is troublesome to find and cancel one by one when canceling merge cells. Here's a simple way to identify merged cells in Excel, identify the merged cells, and then cancel the merging of the cells, as well as other actions such as formatting.
To show, here I used a template Excel file, which contains two merge regions, such as:
Code use:
Step 1 : instantiates an Workbook object and loads an Excel document.
New Workbook (); Workbook. LoadFromFile (" test. xlsx");
Step 2 : Gets the first worksheet of an Excel document.
Worksheet sheet = workbook. worksheets[0];
Step 3 : Gets the merge region and saves the result to a CellRange array.
cellrange[] Range = sheet. Mergedcells;
Step 4 : iterates through the array and merges the cells in the merged range.
foreach inch range) { cell. UnMerge ();}
Step 5 : saves the document.
Workbook. SaveToFile (" output. xlsx");
:
Full code:
usingSpire.xls;namespacedetect_merged_cells{classProgram {Static voidMain (string[] args) {Workbook Workbook=NewWorkbook (); Workbook. LoadFromFile ("test. xlsx"); Worksheet sheet= Workbook. worksheets[0]; Cellrange[] Range=sheet. Mergedcells; foreach(CellRange cellinchrange) {cell. UnMerge (); } workbook. SaveToFile ("output. xlsx"); } }}
Here I use a free version of Excel component, can see this method is very simple, only need a few lines of code, interested friends can download from NuGet: https://www.nuget.org/packages/FreeSpire.XLS/.
C # Get merged cells in Excel