PPTX

表格

创建带合并单元格、自定义样式和格式化内容的表格

使用 TableFrame 向幻灯片添加结构化的表格数据。

基本表格

import { Slide, TableFrame, Paragraph, TextRun } from "@office-open/pptx";

new Slide({
    children: [
        new TableFrame({
            x: 1,
            y: 1,
            width: 8,
            height: 3,
            rows: [
                {
                    cells: [
                        { text: "Name", options: { bold: true } },
                        { text: "Role", options: { bold: true } },
                        { text: "Department", options: { bold: true } },
                    ],
                    header: true,
                },
                { cells: [{ text: "Alice" }, { text: "Engineer" }, { text: "Dev" }] },
                { cells: [{ text: "Bob" }, { text: "Designer" }, { text: "UX" }] },
                { cells: [{ text: "Carol" }, { text: "Manager" }, { text: "Ops" }] },
            ],
        }),
    ],
});

单元格格式

每个单元格支持文本格式选项:

new TableFrame({
    x: 1,
    y: 1,
    width: 8,
    height: 3,
    rows: [
        {
            cells: [
                {
                    text: "Header",
                    options: { bold: true, fontSize: 14, color: "FFFFFF" },
                    fill: { type: "solid", color: "2E74B5" },
                    align: "center",
                    valign: "middle",
                },
            ],
            header: true,
        },
        {
            cells: [
                {
                    text: "Data cell",
                    options: { fontSize: 12 },
                    fill: { type: "solid", color: "F2F2F2" },
                },
            ],
        },
    ],
});

丰富的单元格内容

单元格可以包含完整的 Paragraph 对象:

import { Paragraph, TextRun } from "@office-open/pptx";

{
  cells: [
    {
      paragraphs: [
        new Paragraph({
          children: [
            new TextRun({ text: "Bold title", bold: true, fontSize: 14 }),
          ],
        }),
        new Paragraph({
          children: [
            new TextRun({ text: "Description text", fontSize: 11, color: "666666" }),
          ],
        }),
      ],
    },
  ],
}

合并单元格

使用 rowSpancolSpan 水平或垂直合并单元格:

new TableFrame({
    x: 1,
    y: 1,
    width: 8,
    height: 4,
    rows: [
        {
            cells: [
                { text: "Category", options: { bold: true }, rowSpan: 2 },
                { text: "Q1", options: { bold: true } },
                { text: "Q2", options: { bold: true } },
            ],
        },
        {
            cells: [
                // 第一个单元格与上方行合并
                { text: "100" },
                { text: "120" },
            ],
        },
        {
            cells: [
                { text: "Products", options: { bold: true }, rowSpan: 2 },
                { text: "200" },
                { text: "250" },
            ],
        },
        {
            cells: [{ text: "210" }, { text: "270" }],
        },
    ],
});

列宽

显式控制列宽:

new TableFrame({
    x: 1,
    y: 1,
    width: 8,
    colWidths: [2, 3, 3], // 每列的宽度(英寸)
    rows: [
        { cells: [{ text: "ID" }, { text: "Name" }, { text: "Email" }] },
        { cells: [{ text: "1" }, { text: "Alice" }, { text: "alice@example.com" }] },
    ],
});

边框

自定义单元格边框:

{
  text: "Cell with border",
  borders: {
    top: { style: "single", size: 2, color: "2E74B5" },
    bottom: { style: "single", size: 2, color: "2E74B5" },
    left: { style: "single", size: 2, color: "2E74B5" },
    right: { style: "single", size: 2, color: "2E74B5" },
  },
}
Copyright © 2026