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.OpenXmlCompositeElement
public 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 OpenXmlCompositeElement
type 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 OpenXmlCompositeElement
Public Class MergeCells
Inherits OpenXmlCompositeElement
Public Class MergeCells
Inherits TypedOpenXmlCompositeElement
- 継承
- 継承
- 属性
例
次のコード例では、既存のスプレッドシート内の 2 つの隣接するセルをマージします。 例を実行した後、ファイルを見て、マージされたセルに注目してください。
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 1st Edition]
mergeCells (セルのマージ)
このコレクションは、シート内のすべてのマージされたセルを表します。
[例:
この例は、3 つの範囲がマージされることを示しています。 マージされた範囲の書式と内容は、常に左上のセルに格納されます。
<mergeCells>
<mergeCell ref="C2:F2"/>
<mergeCell ref="B19:C20"/>
<mergeCell ref="E19:G19"/>
</mergeCells>
例終わり]
| 親の要素 |
|---|
| worksheet (§18.3.1.99) |
| 子要素 | Subclause |
|---|---|
| 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) |
現在の要素の子要素の 1 つを別の 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) |