Word for UWP
表の挿入
Word for UWP の操作 > 上級レベルの操作 > 表の挿入

表は、Word ドキュメントでデータをいくつかの行と列に整えて表示するために使用されます。Word for UWP を使用すると、次のコードで表を Word ドキュメントに追加できます。

' 表を追加します
word.LineBreak()
Dim rows As Integer = 4
Dim cols As Integer = 2
Dim table As New RtfTable(rows, cols)
word.Add(table)
table.Rows(0).Cells(0).SetMerged(1, 2)

For row As Integer = 0 To rows - 1
        If row = 0 Then
                table.Rows(row).Height = 50
        End If
        For col As Integer = 0 To cols - 1
                If row = 0 AndAlso col = 0 Then
                        text = Strings.DocumentBasicText2
                        table.Rows(row).Cells(col).Alignment = ContentAlignment.MiddleCenter
                        table.Rows(row).Cells(col).BackFilling = Colors.LightPink
                Else
                        text = String.Format("表のセル{0}:{1}", row, col)
                        table.Rows(row).Cells(col).BackFilling = Colors.LightYellow
                End If
                table.Rows(row).Cells(col).Content.Add(New RtfString(text))
                table.Rows(row).Cells(col).BottomBorderWidth = 2
                table.Rows(row).Cells(col).TopBorderWidth = 2
                table.Rows(row).Cells(col).LeftBorderWidth = 2
                table.Rows(row).Cells(col).RightBorderWidth = 2
                If col = cols - 1 Then
                        table.Rows(row).Cells(col).Alignment = ContentAlignment.BottomRight
                End If
        Next
Next
// 表を追加します
word.LineBreak();
int rows = 4;
int cols = 2;
RtfTable table = new RtfTable(rows, cols);
word.Add(table);
table.Rows[0].Cells[0].SetMerged(1, 2);

for (int row = 0; row < rows; row++) {
  if (row == 0) {
    table.Rows[row].Height = 50;
  }
  for (int col = 0; col < cols; col++) {
    if (row == 0 && col == 0) {
      text = Strings.DocumentBasicText2;
      table.Rows[row].Cells[col].Alignment = ContentAlignment.MiddleCenter;
      table.Rows[row].Cells[col].BackFilling = Colors.LightPink;
    } else {
      text = string.Format("表のセル{0}:{1}", row, col);
      table.Rows[row].Cells[col].BackFilling = Colors.LightYellow;
    }
    table.Rows[row].Cells[col].Content.Add(new RtfString(text));
    table.Rows[row].Cells[col].BottomBorderWidth = 2;
    table.Rows[row].Cells[col].TopBorderWidth = 2;
    table.Rows[row].Cells[col].LeftBorderWidth = 2;
    table.Rows[row].Cells[col].RightBorderWidth = 2;
    if (col == cols - 1) {
      table.Rows[row].Cells[col].Alignment = ContentAlignment.BottomRight;
    }
  }
}

上記のコードが作成する表は、Word ドキュメント内に適切にインデントされて配置されます。

上記のコードの出力は、次の図のようになります。