DOCX

表格

创建包含行、单元格、合并单元格、边框和样式的表格

使用 tablecellschildren 属性向文档中添加结构化数据。

基本表格

{
  "sections": [
    {
      "children": [
        {
          "table": {
            "rows": [
              {
                "cells": [
                  { "children": [{ "paragraph": { "children": [{ "text": "Name" }] } }] },
                  { "children": [{ "paragraph": { "children": [{ "text": "Age" }] } }] }
                ]
              },
              {
                "cells": [
                  { "children": [{ "paragraph": { "children": [{ "text": "Alice" }] } }] },
                  { "children": [{ "paragraph": { "children": [{ "text": "30" }] } }] }
                ]
              }
            ]
          }
        }
      ]
    }
  ]
}

列宽

{
  "sections": [
    {
      "children": [
        {
          "table": {
            "columnWidths": [3000, 2000, 2000],
            "rows": [
              {
                "cells": [
                  {
                    "children": [{ "paragraph": { "children": ["Col 1"] } }],
                    "width": { "size": 3000, "type": "dxa" }
                  },
                  {
                    "children": [{ "paragraph": { "children": ["Col 2"] } }],
                    "width": { "size": 2000, "type": "dxa" }
                  },
                  {
                    "children": [{ "paragraph": { "children": ["Col 3"] } }],
                    "width": { "size": 2000, "type": "dxa" }
                  }
                ]
              }
            ]
          }
        }
      ]
    }
  ]
}

合并单元格

使用 verticalMerge 进行垂直合并,使用 columnSpan 进行水平合并:

{
  "sections": [
    {
      "children": [
        {
          "table": {
            "rows": [
              {
                "cells": [
                  {
                    "verticalMerge": "RESTART",
                    "children": [{ "paragraph": { "children": ["Merged (2 rows)"] } }]
                  },
                  { "children": [{ "paragraph": { "children": ["B1"] } }] }
                ]
              },
              {
                "cells": [
                  {
                    "verticalMerge": "CONTINUE",
                    "children": [{ "paragraph": { "children": [] } }]
                  },
                  { "children": [{ "paragraph": { "children": ["B2"] } }] }
                ]
              }
            ]
          }
        }
      ]
    }
  ]
}

表格边框

{
  "sections": [
    {
      "children": [
        {
          "table": {
            "borders": {
              "top": { "style": "single", "size": 1, "color": "000000" },
              "bottom": { "style": "single", "size": 1, "color": "000000" },
              "left": { "style": "single", "size": 1, "color": "000000" },
              "right": { "style": "single", "size": 1, "color": "000000" },
              "insideHorizontal": { "style": "single", "size": 1, "color": "000000" },
              "insideVertical": { "style": "single", "size": 1, "color": "000000" }
            },
            "rows": [
              {
                "cells": [
                  { "children": [{ "paragraph": { "children": ["A"] } }] },
                  { "children": [{ "paragraph": { "children": ["B"] } }] }
                ]
              },
              {
                "cells": [
                  { "children": [{ "paragraph": { "children": ["C"] } }] },
                  { "children": [{ "paragraph": { "children": ["D"] } }] }
                ]
              }
            ]
          }
        }
      ]
    }
  ]
}

单元格格式

为单个单元格设置背景、边框和垂直对齐:

{
  "sections": [
    {
      "children": [
        {
          "table": {
            "rows": [
              {
                "cells": [
                  {
                    "shading": { "fill": "F2F2F2" },
                    "verticalAlign": "center",
                    "borders": {
                      "top": { "style": "single", "size": 1, "color": "CCCCCC" },
                      "bottom": { "style": "single", "size": 1, "color": "CCCCCC" },
                      "left": { "style": "single", "size": 1, "color": "CCCCCC" },
                      "right": { "style": "single", "size": 1, "color": "CCCCCC" }
                    },
                    "children": [{ "paragraph": { "children": ["Styled cell"] } }]
                  }
                ]
              }
            ]
          }
        }
      ]
    }
  ]
}

表格宽度模式

控制表格如何占用页面宽度:

{
  "sections": [
    {
      "children": [
        {
          "table": {
            "width": { "size": 100, "type": "pct" },
            "rows": [
              {
                "cells": [{ "children": [{ "paragraph": { "children": ["Full width table"] } }] }]
              }
            ]
          }
        }
      ]
    }
  ]
}

宽度类型:

  • "dxa" — 固定宽度(TWIP)
  • "pct" — 页面宽度百分比(100 = 100%)
  • "auto" — 自动适应内容

Table 选项参考

选项类型说明
rowsTableRowOptions[]表格行
widthobject表格宽度({ size, type }
columnWidthsnumber[]各列宽度(TWIP)
bordersobject所有边的边框定义
floatobject浮动表格定位
Copyright © 2026