简体   繁体   中英

how to check if excel worksheet has merged cells using c#

I want to check if excel worksheet using c# has merged cells I searched and found many ways that talking about how to merge or Unmerged cell or check if there is cell within fix range of sheet like [a1:100] what I actually want is to

check if there is any merged cells in all sheets and all cells within all excel file or at least the sheet I am working

thanks for help in advanced

You might be interested in using the following method

Function CountMerged(pWorkRng As Range) As Long
'Updateby20140307
Dim rng As Range
Dim total As Long
Set dt = CreateObject("Scripting.Dictionary")
For Each rng In pWorkRng
    If rng.MergeCells Then
        TempAddress = rng.MergeArea.Address
        dt(TempAddress) = ""
    End If
Next
CountMerged = dt.Count
End Function

Source: https://www.extendoffice.com/documents/excel/1501-excel-count-merged-cells.html

Hope that this will help you

The following approach will be of help and it's in c#:

using Spire.Xls;

namespace Detect_Merged_Cells
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            CellRange[] range = sheet.MergedCells;
            foreach (CellRange cell in range)
            {
                cell.UnMerge();
            }
            workbook.SaveToFile("Output.xlsx",ExcelVersion.Version2010);
        }
    }
}

The above solution is based on a 3rd party library - Spire.XLS (you can get it from nuget and note I work for Spire). For more information, check this link: https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Cells/How-to-detect-merged-cells-in-a-worksheet.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM