NamedRange.Merge 方法

NamedRange 控件创建一个合并单元格。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)

语法

声明
Sub Merge ( _
    Across As Object _
)
void Merge(
    Object Across
)

参数

  • Across
    类型:System.Object
    如果将指定范围中每一行中的单元格作为单独的合并单元格来合并,则为 true。默认值为 false。

备注

合并范围的值是在 NamedRange 控件左上角中的单元格中指定的。

可选参数

有关可选参数的信息,请参见Office 解决方案中的可选参数

示例

下面的代码示例在使用 Merge 方法合并包含单个单元格 NamedRange 的多单元格 NamedRange 之前和之后,显示单个单元格 NamedRangeMergeAreaMergeCells 属性的值。

此示例针对的是文档级自定义项。

    Private Sub MergeRange()
        Dim namedRange1 As Microsoft.Office.Tools.Excel.NamedRange _
            = Me.Controls.AddNamedRange(Me.Range("A1", "A5"), _
            "namedRange1")

        Dim namedRange2 As Microsoft.Office.Tools.Excel.NamedRange _
            = Me.Controls.AddNamedRange(Me.Range("A1"), "namedRange2")

        Dim beforeMergeAddress As String = _
            namedRange2.MergeArea.Address(, , _
            Excel.XlReferenceStyle.xlA1, , )

        MessageBox.Show("Before merging, the MergeArea property is '" & _
            beforeMergeAddress & "' and the MergeCells property is " & _
            " '" & namedRange2.MergeCells.ToString() & "'.")

        namedRange1.Select()
        namedRange1.Merge(False)

        Dim afterMergeAddress As String = _
            namedRange2.MergeArea.Address(, , _
            Excel.XlReferenceStyle.xlA1, , )

        MessageBox.Show("After merging, the MergeArea property is '" & _
            afterMergeAddress & "' and the MergeCells property is " & _
            " '" & namedRange2.MergeCells.ToString() & "'.")
    End Sub

private void MergeRange()
{
    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
        this.Controls.AddNamedRange(this.Range["A1", "A5"],
        "namedRange1");

    Microsoft.Office.Tools.Excel.NamedRange namedRange2 =
        this.Controls.AddNamedRange(this.Range["A1"],
        "namedRange2");

    string beforeMergeAddress = namedRange2.MergeArea.get_Address(
        Excel.XlReferenceStyle.xlA1);

    MessageBox.Show("Before merging, the MergeArea property is '" +
        beforeMergeAddress + "' and the MergeCells property is " +
        " '" + namedRange2.MergeCells.ToString() + "'.");

    namedRange1.Select();
    namedRange1.Merge(false);

    string afterMergeAddress = namedRange2.MergeArea.get_Address(
        Excel.XlReferenceStyle.xlA1);

    MessageBox.Show("After merging, the MergeArea property is '" +
        afterMergeAddress + "' and the MergeCells property is " +
        " '" + namedRange2.MergeCells.ToString() + "'.");
}

.NET Framework 安全性

请参见

参考

NamedRange 接口

Microsoft.Office.Tools.Excel 命名空间