C # Get merged cells in Excel

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.