スプレッドシート ドキュメント内の隣接する 2 つのセルを結合する
このトピックでは、Open XML SDK for Office のクラスを使用して、スプレッドシート ドキュメント内の 2 つの隣接するセルをプログラムでマージする方法について説明します。
SpreadsheetDocument オブジェクトを取得する
Open XML SDK では、 SpreadsheetDocument クラスは Excel ドキュメント パッケージを表します。 Excel ドキュメントを開いて操作するには、ドキュメントから SpreadsheetDocument
クラスのインスタンスを作成します。
ドキュメントからインスタンスを作成した後、ワークシートが入っているメインのブック パーツにアクセスできます。 ドキュメント内のテキストは、 SpreadsheetML
マークアップを使用して XML としてパッケージ内で表されます。
ドキュメントからクラス インスタンスを作成するには、 Open メソッドのいずれかを呼び出します。 メソッドにはいくつかの種類があり、それぞれシグネチャが異なります。 このトピックのサンプル コードでは、2 つのパラメーターを必要とするシグネチャを持つ Open(String, Boolean) メソッドを使用します。 最初のパラメーターは、開くドキュメントを表す完全なパスの文字列を受け取ります。 2 番目のパラメーターは、 true
または false
であり、編集のためにファイルを開くかどうかを表します。 このパラメーターが false
されている場合、ドキュメントに加えた変更は保存されません。
spreadsheetML ドキュメントの基本的な構造
SpreadsheetML
ドキュメントの基本的なドキュメント構造は、ブック内のワークシートを参照するSheets要素とSheet要素で構成されます。 ワークシートごとに、それぞれの XML ファイルが作成されます。 たとえば、MySheet1 と MySheet2 という名前の 2 つのワークシートがあるWorkbookのSpreadsheetML
は、Workbook.xml ファイルにあり、次のコード例に示されています。
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<sheets>
<sheet name="MySheet1" sheetId="1" r:id="rId1" />
<sheet name="MySheet2" sheetId="2" r:id="rId2" />
</sheets>
</workbook>
ワークシート XML ファイルには、セル テーブルを表 SheetData 1 つ以上のブロック レベル要素が含まれており、1 つ以上の Row 要素が含まれています。
row
には、1 つ以上のCell要素が含まれています。 各セルには、セルの値を表す CellValue 要素が含まれています。 たとえば、セル A1 の値が 100 しかないブックの最初のワークシートの SpreadsheetML
は、Sheet1.xml ファイルにあり、次のコード例に示されています。
<?xml version="1.0" encoding="UTF-8" ?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetData>
<row r="1">
<c r="A1">
<v>100</v>
</c>
</row>
</sheetData>
</worksheet>
Open XML SDK を使用すると、 SpreadsheetML
要素に対応する厳密に型指定されたクラスを使用するドキュメント構造とコンテンツを作成できます。 これらのクラスは、 DocumentFormat.OpenXML.Spreadsheet
名前空間にあります。 次の表に、 workbook
、 sheets
、 sheet
、 worksheet
、 sheetData
の各要素に対応するクラスのクラス名を示します。
SpreadsheetML の要素 | Open XML SDK クラス | 説明 |
---|---|---|
<workbook/> |
DocumentFormat.OpenXML.Spreadsheet.Workbook | メイン ドキュメント パーツのルート要素。 |
<sheets/> |
DocumentFormat.OpenXML.Spreadsheet.Sheets | ISO/IEC 29500 の仕様で規定されている、シート、ファイル バージョン、その他のブロック レベル構造のコンテナー。 |
<sheet/> |
DocumentFormat.OpenXml.Spreadsheet.Sheet | シート定義ファイルを指し示すシート。 |
<worksheet/> |
DocumentFormat.OpenXML.Spreadsheet。 Worksheet | シート データが含まれているシート定義ファイル。 |
<sheetData/> |
DocumentFormat.OpenXML.Spreadsheet.SheetData | セルの表。行ごとにグループ化されています。 |
<row/> |
DocumentFormat.OpenXml.Spreadsheet.Row | セルの表内の行。 |
<c/> |
DocumentFormat.OpenXml.Spreadsheet.Cell | 行内のセル。 |
<v/> |
DocumentFormat.OpenXml.Spreadsheet.CellValue | セルの値。 |
サンプル コードの動作のしくみ
編集のためにスプレッドシート ファイルを開いた後、コードは指定したセルが存在することを確認し、存在しない場合は、 CreateSpreadsheetCellIfNotExist
メソッドを呼び出して作成し、適切な Row オブジェクトに追加します。
// Given a Worksheet and a cell name, verifies that the specified cell exists.
// If it does not exist, creates a new cell.
static void CreateSpreadsheetCellIfNotExist(Worksheet worksheet, string cellName)
{
string columnName = GetColumnName(cellName);
uint rowIndex = GetRowIndex(cellName);
IEnumerable<Row> rows = worksheet.Descendants<Row>().Where(r => r.RowIndex?.Value == rowIndex);
// If the Worksheet does not contain the specified row, create the specified row.
// Create the specified cell in that row, and insert the row into the Worksheet.
if (rows.Count() == 0)
{
Row row = new Row() { RowIndex = new UInt32Value(rowIndex) };
Cell cell = new Cell() { CellReference = new StringValue(cellName) };
row.Append(cell);
worksheet.Descendants<SheetData>().First().Append(row);
}
else
{
Row row = rows.First();
IEnumerable<Cell> cells = row.Elements<Cell>().Where(c => c.CellReference?.Value == cellName);
// If the row does not contain the specified cell, create the specified cell.
if (cells.Count() == 0)
{
Cell cell = new Cell() { CellReference = new StringValue(cellName) };
row.Append(cell);
}
}
}
列名を取得するため、セル名のうち列名の部分に一致する新しい正規表現を作成します。 この正規表現は、英大文字と英小文字の任意の組み合わせに一致します。 正規表現の詳細については、「正規表現言語要素」をご覧ください。 このコードでは、Regex.Match を呼び出すことにより、列名を取得します。
// Given a cell name, parses the specified cell to get the column name.
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;
}
行のインデックスを取得するために、セル名の行インデックス部分を照合する新しい正規表現を作成します。 この正規表現は、10 進数の組み合わせ部分を照合します。 次のコードは、10 進数で構成される、セル名の行インデックス部分を照合する正規表現を作成します。
// Given a cell name, parses the specified cell to get the row index.
static uint GetRowIndex(string cellName)
{
// Create a regular expression to match the row index portion the cell name.
Regex regex = new Regex(@"\d+");
Match match = regex.Match(cellName);
return uint.Parse(match.Value);
}
サンプル コード
次のコードは、 Row ドキュメント パッケージ内の 2 つの隣接するセルをマージします。 2 つのセルを結合する場合、一方のセルの内容だけが保持されます。 左から右へ書く言語の場合、左上隅のセルの内容が保持されます。 右から左へ書く言語の場合、右上隅のセルの内容が保持されます。
以下に、C# と Visual Basic による完全なサンプル コードを示します。
static void MergeTwoCells(string docName, string sheetName, string cell1Name, string cell2Name)
{
// Open the document for editing.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true))
{
Worksheet? worksheet = GetWorksheet(document, sheetName);
if (worksheet is null || string.IsNullOrEmpty(cell1Name) || string.IsNullOrEmpty(cell2Name))
{
return;
}
// Verify if the specified cells exist, and if they do not exist, create them.
CreateSpreadsheetCellIfNotExist(worksheet, cell1Name);
CreateSpreadsheetCellIfNotExist(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 if (worksheet.Elements<DataConsolidate>().Count() > 0)
{
worksheet.InsertAfter(mergeCells, worksheet.Elements<DataConsolidate>().First());
}
else if (worksheet.Elements<SortState>().Count() > 0)
{
worksheet.InsertAfter(mergeCells, worksheet.Elements<SortState>().First());
}
else if (worksheet.Elements<AutoFilter>().Count() > 0)
{
worksheet.InsertAfter(mergeCells, worksheet.Elements<AutoFilter>().First());
}
else if (worksheet.Elements<Scenarios>().Count() > 0)
{
worksheet.InsertAfter(mergeCells, worksheet.Elements<Scenarios>().First());
}
else if (worksheet.Elements<ProtectedRanges>().Count() > 0)
{
worksheet.InsertAfter(mergeCells, worksheet.Elements<ProtectedRanges>().First());
}
else if (worksheet.Elements<SheetProtection>().Count() > 0)
{
worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetProtection>().First());
}
else if (worksheet.Elements<SheetCalculationProperties>().Count() > 0)
{
worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetCalculationProperties>().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);
}
}
// Given a Worksheet and a cell name, verifies that the specified cell exists.
// If it does not exist, creates a new cell.
static void CreateSpreadsheetCellIfNotExist(Worksheet worksheet, string cellName)
{
string columnName = GetColumnName(cellName);
uint rowIndex = GetRowIndex(cellName);
IEnumerable<Row> rows = worksheet.Descendants<Row>().Where(r => r.RowIndex?.Value == rowIndex);
// If the Worksheet does not contain the specified row, create the specified row.
// Create the specified cell in that row, and insert the row into the Worksheet.
if (rows.Count() == 0)
{
Row row = new Row() { RowIndex = new UInt32Value(rowIndex) };
Cell cell = new Cell() { CellReference = new StringValue(cellName) };
row.Append(cell);
worksheet.Descendants<SheetData>().First().Append(row);
}
else
{
Row row = rows.First();
IEnumerable<Cell> cells = row.Elements<Cell>().Where(c => c.CellReference?.Value == cellName);
// If the row does not contain the specified cell, create the specified cell.
if (cells.Count() == 0)
{
Cell cell = new Cell() { CellReference = new StringValue(cellName) };
row.Append(cell);
}
}
}
// Given a SpreadsheetDocument and a worksheet name, get the specified worksheet.
static Worksheet? GetWorksheet(SpreadsheetDocument document, string worksheetName)
{
WorkbookPart workbookPart = document.WorkbookPart ?? document.AddWorkbookPart();
IEnumerable<Sheet> sheets = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == worksheetName);
string? id = sheets.First().Id;
WorksheetPart? worksheetPart = id is not null ? (WorksheetPart)workbookPart.GetPartById(id) : null;
return worksheetPart?.Worksheet;
}
// Given a cell name, parses the specified cell to get the column name.
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;
}
// Given a cell name, parses the specified cell to get the row index.
static uint GetRowIndex(string cellName)
{
// Create a regular expression to match the row index portion the cell name.
Regex regex = new Regex(@"\d+");
Match match = regex.Match(cellName);
return uint.Parse(match.Value);
}