MergeCells 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义 MergeCells 类。
此类在 Office 2007 及更高版本中提供。
将对象序列化为 xml 时,其限定名称为 x:mergeCells。
[DocumentFormat.OpenXml.ChildElementInfo(typeof(DocumentFormat.OpenXml.Spreadsheet.MergeCell))]
[DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)]
[DocumentFormat.OpenXml.SchemaAttr(22, "mergeCells")]
public class MergeCells : DocumentFormat.OpenXml.OpenXmlCompositeElementpublic class MergeCells : DocumentFormat.OpenXml.OpenXmlCompositeElement[DocumentFormat.OpenXml.SchemaAttr(22, "mergeCells")]
public class MergeCells : DocumentFormat.OpenXml.OpenXmlCompositeElement[DocumentFormat.OpenXml.SchemaAttr("x:mergeCells")]
public class MergeCells : DocumentFormat.OpenXml.OpenXmlCompositeElement[DocumentFormat.OpenXml.SchemaAttr("x:mergeCells")]
public class MergeCells : DocumentFormat.OpenXml.TypedOpenXmlCompositeElement[DocumentFormat.OpenXml.ChildElementInfo(typeof(DocumentFormat.OpenXml.Spreadsheet.MergeCell))]
public class MergeCells : DocumentFormat.OpenXml.OpenXmlCompositeElement[DocumentFormat.OpenXml.ChildElementInfo(typeof(DocumentFormat.OpenXml.Spreadsheet.MergeCell))]
[DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)]
public class MergeCells : DocumentFormat.OpenXml.OpenXmlCompositeElement[<DocumentFormat.OpenXml.ChildElementInfo(typeof(DocumentFormat.OpenXml.Spreadsheet.MergeCell))>]
[<DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)>]
[<DocumentFormat.OpenXml.SchemaAttr(22, "mergeCells")>]
type MergeCells = class
    inherit OpenXmlCompositeElementtype MergeCells = class
    inherit OpenXmlCompositeElement[<DocumentFormat.OpenXml.SchemaAttr(22, "mergeCells")>]
type MergeCells = class
    inherit OpenXmlCompositeElement[<DocumentFormat.OpenXml.SchemaAttr("x:mergeCells")>]
type MergeCells = class
    inherit OpenXmlCompositeElement[<DocumentFormat.OpenXml.SchemaAttr("x:mergeCells")>]
type MergeCells = class
    inherit TypedOpenXmlCompositeElement[<DocumentFormat.OpenXml.ChildElementInfo(typeof(DocumentFormat.OpenXml.Spreadsheet.MergeCell))>]
type MergeCells = class
    inherit OpenXmlCompositeElement[<DocumentFormat.OpenXml.ChildElementInfo(typeof(DocumentFormat.OpenXml.Spreadsheet.MergeCell))>]
[<DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)>]
type MergeCells = class
    inherit OpenXmlCompositeElementPublic Class MergeCells
Inherits OpenXmlCompositeElementPublic Class MergeCells
Inherits TypedOpenXmlCompositeElement- 继承
- 继承
- 属性
示例
下面的代码示例合并现有电子表格中的两个相邻单元格。 运行示例后,查看文件并注意合并的单元格。
using System;  
using System.Collections.Generic;  
using System.Linq;  
using DocumentFormat.OpenXml;  
using DocumentFormat.OpenXml.Packaging;  
using DocumentFormat.OpenXml.Spreadsheet;  
using System.Text.RegularExpressions;  
namespace MergeCellsEx  
{  
    class Program  
    {  
        // Merge two adjacent cells in a worksheet.  
        // Notice that after the merge, only the content from one cell is preserved.  
        static void Main(string[] args)  
        {  
            string docName = @"C:\Users\Public\Documents\MergeCellsEx.xlsx";  
            string sheetName = "mySheet";  
            string cell1Name = "A2";  
            string cell2Name = "B2";  
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true))  
            {  
                Worksheet worksheet = GetWorksheet(document, sheetName);  
                // Create Spreadsheet cells.  
                CreateSpreadsheetCell(worksheet, cell1Name);  
                CreateSpreadsheetCell(worksheet, cell2Name);  
                MergeCells mergeCells;  
                if (worksheet.Elements<MergeCells>().Count() > 0)  
                    mergeCells = worksheet.Elements<MergeCells>().First();  
                else  
                {  
                    mergeCells = new MergeCells();  
                    // Insert a MergeCells object into the specified position.  
                    if (worksheet.Elements<CustomSheetView>().Count() > 0)  
                         worksheet.InsertAfter(mergeCells, worksheet.Elements<CustomSheetView>().First());  
                    else  
                        worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetData>().First());  
                }  
                // Create the merged cell and append it to the MergeCells collection.  
                MergeCell mergeCell = new MergeCell() { Reference =   
                    new StringValue(cell1Name + ":" + cell2Name) };  
                mergeCells.Append(mergeCell);  
                worksheet.Save();  
            }  
            Console.WriteLine("The two cells are now merged.\nPress a key.");  
            Console.ReadKey();  
        }  
        // Get the specified worksheet.  
        private static Worksheet GetWorksheet(SpreadsheetDocument document, string worksheetName)  
        {  
            IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook  
                .Descendants<Sheet>().Where(s => s.Name == worksheetName);  
            WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart  
                .GetPartById(sheets.First().Id);  
            return worksheetPart.Worksheet;  
        }  
        // Create a spreadsheet cell.   
        private static void CreateSpreadsheetCell(Worksheet worksheet, string cellName)  
        {  
            string columnName = GetColumnName(cellName);  
            uint rowIndex = 2;  
            IEnumerable<Row> rows = worksheet.Descendants<Row>().Where(r => r  
                .RowIndex.Value == rowIndex);  
            Row row = rows.First();  
            IEnumerable<Cell> cells = row.Elements<Cell>().Where(c => c.CellReference  
                .Value == cellName);  
        }  
        // Parse the cell name to get the column name.  
        private static string GetColumnName(string cellName)  
        {  
            // Create a regular expression to match the column name portion of the cell name.  
            Regex regex = new Regex("[A-Za-z]+");  
            Match match = regex.Match(cellName);  
            return match.Value;  
        }  
    }  
}  
Imports System.Collections.Generic  
Imports System.Linq  
Imports DocumentFormat.OpenXml  
Imports DocumentFormat.OpenXml.Packaging  
Imports DocumentFormat.OpenXml.Spreadsheet  
Imports System.Text.RegularExpressions  
Module Module1  
    ' Merge two adjacent cells in a worksheet.  
    ' Notice that after the merge, only the content from one cell is preserved.  
    Sub Main(ByVal args As String())  
        Dim docName As String = "C:\Users\Public\Documents\MergeCellsEx.xlsx"  
        Dim sheetName As String = "mySheet"  
        Dim cell1Name As String = "A2"  
        Dim cell2Name As String = "B2"  
        Using document As SpreadsheetDocument = SpreadsheetDocument.Open(docName, True)  
            Dim worksheet As Worksheet = GetWorksheet(document, sheetName)  
            ' Create Spreadsheet cells.  
            CreateSpreadsheetCell(worksheet, cell1Name)  
            CreateSpreadsheetCell(worksheet, cell2Name)  
            Dim mergeCells As MergeCells  
            If worksheet.Elements(Of MergeCells)().Count() > 0 Then  
                mergeCells = worksheet.Elements(Of MergeCells)().First()  
            Else  
                mergeCells = New MergeCells()  
                ' Insert a MergeCells object into the specified position.  
                If worksheet.Elements(Of CustomSheetView)().Count() > 0 Then  
                    worksheet.InsertAfter(mergeCells, worksheet.Elements(Of CustomSheetView)().First())  
                Else  
                    worksheet.InsertAfter(mergeCells, worksheet.Elements(Of SheetData)().First())  
                End If  
            End If  
            ' Create the merged cell and append it to the MergeCells collection.  
            Dim mergeCell As New MergeCell() With { _  
             .Reference = New StringValue(cell1Name & ":" & cell2Name) _  
            }  
            mergeCells.Append(mergeCell)  
            worksheet.Save()  
        End Using  
        Console.WriteLine("The two cells are now merged." & vbLf & "Press a key.")  
        Console.ReadKey()  
    End Sub  
    ' Get the specified worksheet.  
    Private Function GetWorksheet(ByVal document As SpreadsheetDocument, ByVal worksheetName As String) As Worksheet  
        Dim sheets As IEnumerable(Of Sheet) = document.WorkbookPart.Workbook.Descendants(Of Sheet)().Where(Function(s) s.Name = worksheetName)  
        Dim worksheetPart As WorksheetPart = DirectCast(document.WorkbookPart.GetPartById(sheets.First().Id), WorksheetPart)  
        Return worksheetPart.Worksheet  
    End Function  
    ' Create a spreadsheet cell.   
    Private Sub CreateSpreadsheetCell(ByVal worksheet As Worksheet, ByVal cellName As String)  
        Dim columnName As String = GetColumnName(cellName)  
        Dim rowIndex As UInteger = 2  
        Dim rows As IEnumerable(Of Row) = worksheet.Descendants(Of Row)().Where(Function(r) r.RowIndex.Value = rowIndex)  
        Dim row As Row = rows.First()  
        Dim cells As IEnumerable(Of Cell) = row.Elements(Of Cell)().Where(Function(c) c.CellReference.Value = cellName)  
    End Sub  
    ' Parse the cell name to get the column name.  
    Private Function GetColumnName(ByVal cellName As String) As String  
        ' Create a regular expression to match the column name portion of the cell name.  
        Dim regex As New Regex("[A-Za-z]+")  
        Dim match As Match = regex.Match(cellName)  
        Return match.Value  
    End Function  
End Module  
注解
[ISO/IEC 29500-1 第 1 版]
mergeCells (合并单元格)
此集合表示工作表中的所有合并单元格。
[示例:
此示例显示合并了三个区域。 合并区域的格式和内容始终存储在左上角的单元格中。
<mergeCells>  
  <mergeCell ref="C2:F2"/>  
  <mergeCell ref="B19:C20"/>  
  <mergeCell ref="E19:G19"/>  
</mergeCells>  
示例结束]
| 父元素 | 
|---|
| worksheet (§18.3.1.99) | 
| 子元素 | 第 | 
|---|---|
| mergeCell (合并单元格) | §18.3.1.54 | 
| 属性 | 说明 | 
|---|---|
| count (Count) | 合并的单元格集合的计数。 此属性的可能值由 W3C XML 架构 unsignedInt 数据类型定义。 | 
[注意:此元素的内容模型 (CT_MergeCells) 的 W3C XML 架构定义位于 §A.2 中。 注释结束]
ISO/IEC29500:2008。
构造函数
| MergeCells() | 初始化 MergeCells 类的新实例。 | 
| MergeCells(IEnumerable<OpenXmlElement>) | 使用指定的子元素初始化 MergeCells 类的新实例。 | 
| MergeCells(OpenXmlElement[]) | 使用指定的子元素初始化 MergeCells 类的新实例。 | 
| MergeCells(String) | 从外部 XML 初始化 MergeCells 类的新实例。 | 
属性
| ChildElements | 获取当前元素的所有子节点。(继承自 OpenXmlElement) | 
| Count | 记数 表示架构中的以下属性:count | 
| ExtendedAttributes | 获取当前元素的架构) 中未定义的所有扩展属性 (属性。(继承自 OpenXmlElement) | 
| Features | IFeatureCollection获取当前元素的 。 此功能集合将是只读的,但会从其父部件和包继承功能(如果可用)。(继承自 OpenXmlElement) | 
| FirstChild | 获取当前 OpenXmlElement 元素的第一个子元素。(继承自 OpenXmlCompositeElement) | 
| HasAttributes | 获取一个值,该值指示当前元素是否具有任何属性。(继承自 OpenXmlElement) | 
| HasChildren | 获取一个值,该值指示当前元素是否具有任何子元素。(继承自 OpenXmlCompositeElement) | 
| InnerText | 获取或设置当前节点及其所有子节点的串联值。(继承自 OpenXmlCompositeElement) | 
| InnerXml | 获取或设置仅表示当前节点的子节点的标记。(继承自 OpenXmlCompositeElement) | 
| LastChild | 获取当前 OpenXmlElement 元素的最后一个子元素。 如果没有这样的 OpenXmlElement 元素,则返回 null (Visual Basic) Nothing。(继承自 OpenXmlCompositeElement) | 
| LocalName | 定义 MergeCells 类。 此类在 Office 2007 及更高版本中提供。 将对象序列化为 xml 时,其限定名称为 x:mergeCells。 | 
| LocalName | 获取当前元素的本地名称。(继承自 OpenXmlElement) | 
| MCAttributes | 获取或设置标记兼容性属性。 如果未为当前元素定义标记兼容性属性,则返回 null。(继承自 OpenXmlElement) | 
| NamespaceDeclarations | 获取在当前元素中定义的所有命名空间声明。 如果没有命名空间声明,则返回空枚举器。(继承自 OpenXmlElement) | 
| NamespaceUri | 获取当前元素的命名空间 URI。(继承自 OpenXmlElement) | 
| OpenXmlElementContext | 获取当前元素的 OpenXmlElementContext。(继承自 OpenXmlElement) | 
| OuterXml | 获取表示当前元素及其所有子元素的标记。(继承自 OpenXmlElement) | 
| Parent | 获取当前元素的父元素。(继承自 OpenXmlElement) | 
| Prefix | 获取当前元素的命名空间前缀。(继承自 OpenXmlElement) | 
| XmlQualifiedName | 获取当前元素的限定名称。(继承自 OpenXmlElement) | 
| XName | 获取当前元素的限定名称。(继承自 OpenXmlElement) | 
方法
| AddAnnotation(Object) | 将 对象添加到当前 OpenXmlElement 元素的批注列表中。(继承自 OpenXmlElement) | 
| AddChild(OpenXmlElement, Boolean) | 如果指定元素是已知的子元素,则将其添加到 元素。 这会根据架构将 元素添加到正确的位置。(继承自 OpenXmlCompositeElement) | 
| AddNamespaceDeclaration(String, String) | 将命名空间声明添加到当前节点。(继承自 OpenXmlElement) | 
| Ancestors() | 枚举当前元素的所有上级。(继承自 OpenXmlElement) | 
| Ancestors<T>() | 仅枚举具有指定类型的当前元素的上级。(继承自 OpenXmlElement) | 
| Annotation(Type) | 从当前 OpenXmlElement 元素获取指定类型的第一个批注对象。(继承自 OpenXmlElement) | 
| Annotation<T>() | 从当前 OpenXmlElement 元素获取指定类型的第一个批注对象。(继承自 OpenXmlElement) | 
| Annotations(Type) | 获取具有当前 OpenXmlElement 元素的指定类型的批注的集合。(继承自 OpenXmlElement) | 
| Annotations<T>() | 获取具有当前 OpenXmlElement 元素的指定类型的批注的集合。(继承自 OpenXmlElement) | 
| Append(IEnumerable<OpenXmlElement>) | 将元素列表中的每个元素追加到当前元素的子元素列表的末尾。(继承自 OpenXmlElement) | 
| Append(OpenXmlElement[]) | 将元素数组中的每个元素追加到当前元素的子元素列表的末尾。(继承自 OpenXmlElement) | 
| AppendChild<T>(T) | 将指定的元素追加到当前元素的子节点列表的末尾。(继承自 OpenXmlCompositeElement) | 
| ClearAllAttributes() | 清除所有属性,包括已知属性和扩展属性。(继承自 OpenXmlElement) | 
| Clone() | 创建当前节点的副本。(继承自 OpenXmlElement) | 
| CloneNode(Boolean) | 创建此节点的重复项。 | 
| Descendants() | 枚举当前元素的所有后代。(继承自 OpenXmlElement) | 
| Descendants<T>() | 枚举类型为 T 的当前元素的所有后代。(继承自 OpenXmlElement) | 
| Elements() | 枚举当前元素的所有子元素。(继承自 OpenXmlElement) | 
| Elements<T>() | 仅枚举具有指定类型的当前元素的子元素。(继承自 OpenXmlElement) | 
| ElementsAfter() | 枚举与当前元素相同的父级的所有同级元素。(继承自 OpenXmlElement) | 
| ElementsBefore() | 枚举当前元素之前且具有与当前元素相同的父级的所有同级元素。(继承自 OpenXmlElement) | 
| GetAttribute(String, String) | 获取具有指定标记名称和命名空间 URI 的 Open XML 属性。(继承自 OpenXmlElement) | 
| GetAttributes() | 获取一个列表,该列表包含所有属性的副本。(继承自 OpenXmlElement) | 
| GetEnumerator() | 返回循环访问子集合的枚举器。(继承自 OpenXmlElement) | 
| GetFirstChild<T>() | 查找类型 T 中的第一个子元素。(继承自 OpenXmlElement) | 
| InsertAfter<T>(T, OpenXmlElement) | 在指定的引用元素之后立即插入指定的元素。(继承自 OpenXmlCompositeElement) | 
| InsertAfterSelf<T>(T) | 在当前元素之后立即插入指定的元素。(继承自 OpenXmlElement) | 
| InsertAt<T>(T, Int32) | 在当前元素的子元素的指定索引处插入指定的元素。(继承自 OpenXmlCompositeElement) | 
| InsertBefore<T>(T, OpenXmlElement) | 将指定的元素紧接在指定的引用元素之前。(继承自 OpenXmlCompositeElement) | 
| InsertBeforeSelf<T>(T) | 将指定的元素紧接在当前元素的前面。(继承自 OpenXmlElement) | 
| IsAfter(OpenXmlElement) | 确定当前元素是否以文档顺序显示在指定元素之后。(继承自 OpenXmlElement) | 
| IsBefore(OpenXmlElement) | 确定当前元素是否按文档顺序显示在指定元素之前。(继承自 OpenXmlElement) | 
| LookupNamespace(String) | 解析当前节点上下文中的命名空间前缀。(继承自 OpenXmlElement) | 
| LookupPrefix(String) | 查找当前元素范围内命名空间 URI 的相应前缀。(继承自 OpenXmlElement) | 
| NextSibling() | 获取紧跟在当前 OpenXmlElement 元素后面的 OpenXmlElement 元素。 如果没有下一个 OpenXmlElement 元素,则返回 null (Visual Basic) Nothing。(继承自 OpenXmlElement) | 
| NextSibling<T>() | 获取具有当前 OpenXmlElement 元素后面的指定类型的 OpenXmlElement 元素。 如果没有下一个 OpenXmlElement,则返回 null (Visual Basic) Nothing。(继承自 OpenXmlElement) | 
| PrependChild<T>(T) | 在当前元素的子节点列表的开头插入指定的元素。(继承自 OpenXmlCompositeElement) | 
| PreviousSibling() | 获取紧接在当前 OpenXmlElement 元素之前的 OpenXmlElement 元素。 如果没有前面的 OpenXmlElement 元素,则返回 null (Visual Basic ) 中 Nothing。(继承自 OpenXmlElement) | 
| PreviousSibling<T>() | 获取位于当前 OpenXmlElement 之前的指定类型的 OpenXmlElement 元素。 如果没有前面的 OpenXmlElement 元素,则返回 null (Visual Basic) Nothing。(继承自 OpenXmlElement) | 
| Remove() | 从其父元素中删除当前元素。(继承自 OpenXmlElement) | 
| RemoveAllChildren() | 删除当前元素的所有子元素。(继承自 OpenXmlCompositeElement) | 
| RemoveAllChildren<T>() | 删除类型为 T 的当前元素的所有子元素。(继承自 OpenXmlElement) | 
| RemoveAnnotations(Type) | 从当前 OpenXmlElement 元素中删除指定类型的注释。(继承自 OpenXmlElement) | 
| RemoveAnnotations<T>() | 从当前 OpenXmlElement 元素中删除具有指定类型的批注。(继承自 OpenXmlElement) | 
| RemoveAttribute(String, String) | 从当前元素中删除 属性。(继承自 OpenXmlElement) | 
| RemoveChild<T>(T) | 删除指定的子元素。(继承自 OpenXmlCompositeElement) | 
| RemoveNamespaceDeclaration(String) | 删除指定前缀的命名空间声明。 如果没有前缀,则不删除任何内容。(继承自 OpenXmlElement) | 
| ReplaceChild<T>(OpenXmlElement, T) | 将当前元素的子元素之一替换为另一个 OpenXmlElement 元素。(继承自 OpenXmlCompositeElement) | 
| SetAttribute(OpenXmlAttribute) | 将特性设置为指定的元素。 如果该属性是已知属性,则设置该特性的值。 如果该属性是扩展属性,则会将“openxmlAttribute”添加到扩展属性列表中。(继承自 OpenXmlElement) | 
| SetAttributes(IEnumerable<OpenXmlAttribute>) | 设置元素的多个属性。 如果属性是已知属性,则会设置该属性的值。 如果属性是扩展属性,则会将“openxmlAttribute”添加到扩展属性列表中。(继承自 OpenXmlElement) | 
| WriteTo(XmlWriter) | 将当前节点保存到指定的 XmlWriter。(继承自 OpenXmlElement) | 
显式接口实现
| IEnumerable.GetEnumerator() | 定义 MergeCells 类。 此类在 Office 2007 及更高版本中提供。 将对象序列化为 xml 时,其限定名称为 x:mergeCells。(继承自 OpenXmlElement) | 
| IEnumerable<OpenXmlElement>.GetEnumerator() | 返回循环访问子集合的枚举器。(继承自 OpenXmlElement) |