简体   繁体   English

如何使用 DocumentFormat.OpenXML 或 ClosedXML C# 从 Excel 获取合并单元格

[英]How to get a merged cell from Excel using DocumentFormat.OpenXML or ClosedXML C#

I need to get the area of the merged cell, the line number on which the area ends in Excel using only DocumentFormat.OpenXml or ClosedXML, how to do this for each cell?我需要仅使用 DocumentFormat.OpenXml 或 ClosedXML 获取合并单元格的区域,该区域以 Excel 结尾的行号,如何为每个单元格执行此操作?

在此处输入图像描述

I found this to be a little clunky but I believe this to be the correct approach.我发现这有点笨拙,但我相信这是正确的方法。

private static void GetMergedCells()
{
    var fileName = $"c:\\temp\\Data.xlsm";

    // Open the document for editing.
    using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
    {
        // Get the WorkbookPart object.
        var workbookPart = document.WorkbookPart;

        // Get the first worksheet in the document.  You can change this as need be.
        var worksheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault();

        // Retrieve the WorksheetPart using the Part ID from the previous "Sheet" object.
        var worksheetPart = (WorksheetPart)workbookPart.GetPartById(worksheet.Id);

        // Retrieve the MergeCells element, this will contain all MergeCell elements.
        var mergeCellsList = worksheetPart.Worksheet.Elements<MergeCells>();

        // Now loop through and spit out each range reference for the merged cells.
        // You'll need to process the range either as a string or turn it into another
        // object that gives you the end row.
        foreach (var mergeCells in mergeCellsList)
        {
            foreach (MergeCell mergeCell in mergeCells)
            {
                Console.WriteLine(mergeCell.Reference);
            }
        }
    }
}

If you couldn't already tell, this is using DocumentFormat.OpenXml.Spreadsheet如果你还不能说,这是使用DocumentFormat.OpenXml.Spreadsheet

Using ClosedXML, this could be done with:使用 ClosedXML,这可以通过以下方式完成:

var ws = workbook.Worksheet("Sheet 1");
var cell = ws.Cell("A2");
var mergedRange = cell.MergedRange();
var lastCell = mergedRange.LastCell();

// or 
var lastCellAddress = mergedRange.RangeAddress.LastAddress;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用DocumentFormat.OpenXml C#获取excel工作表中的特定列? - How can I get the specific column in excel worksheet using DocumentFormat.OpenXml C#? 从 excel 单元格中检索数据,即 C# documentformat.openxml 中的 integer.xslx 文档 - Retrieving data from excel cell that is integer .xslx document in C# documentformat.openxml 使用Documentformat.OpenXML在C#中验证Excel文件 - Validate Excel file in c# using Documentformat.OpenXML 如何使用 C# 和 DocumentFormat.OpenXml nuget ZEFE90A8E604A7C84A70E8 格式化 Excel 中的数字 - How to format number in Excel by using C# and DocumentFormat.OpenXml nuget package? 使用DocumentFormat.OpenXml(C#)更新.docx文档中的目录 - Update TOC in .docx document using DocumentFormat.OpenXml (C#) 使用DocumentFormat.OpenXml(c#)合并Word文档 - Merging word-documents using DocumentFormat.OpenXml(c#) 如何使用 DocumentFormat.OpenXml 使用 SharedStringTablePart 创建 excel 文件 - How to create excel file with SharedStringTablePart using DocumentFormat.OpenXml 使用Microsoft DocumentFormat.OpenXml SDK在C#中读取Excel文件 - Reading excel file in c# using Microsoft DocumentFormat.OpenXml SDK 使用DocumentFormat.OpenXml从单词获取文本 - get text from word using DocumentFormat.OpenXml 如何使用 DocumentFormat.OpenXml 总是跳过指定的行数 - How to always skip specified number of Rows using DocumentFormat.OpenXml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM