管理表格

PowerPoint 中的表格是一种高效显示和呈现信息的方式。以网格形式(按行和列排列)的单元格信息直观且易于理解。

Aspose.Slides 提供了 Table 类、Table 类、Cell 类、Cell 类以及其他类型,以便您在各种演示文稿中创建、更新和管理表格。

从头创建表格

  1. 创建一个 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 定义 columnWidth 数组。
  4. 定义 rowHeight 数组。
  5. 通过 addTable 方法向幻灯片添加一个 Table 对象。
  6. 遍历每个 Cell ,对其上、下、左、右边框应用格式。
  7. 合并表格第一行的前两个单元格。
  8. 访问 Cell’s TextFrame
  9. TextFrame 添加一些文本。
  10. 保存修改后的演示文稿。

以下 JavaScript 代码演示了如何在演示文稿中创建表格:

// 实例化表示 PPTX 文件的 Presentation 类
var pres = new aspose.slides.Presentation();
try {
    // 访问第一张幻灯片
    var sld = pres.getSlides().get_Item(0);
    // 定义列宽和行高
    var dblCols = java.newArray("double", [50, 50, 50]);
    var dblRows = java.newArray("double", [50, 30, 30, 30, 30]);
    // 向幻灯片添加表格形状
    var tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
    // 为每个单元格设置边框格式
    for (var row = 0; row < tbl.getRows().size(); row++) {
        for (var cell = 0; cell < tbl.getRows().get_Item(row).size(); cell++) {
            var cellFormat = tbl.getRows().get_Item(row).get_Item(cell).getCellFormat();
            cellFormat.getBorderTop().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
            cellFormat.getBorderTop().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
            cellFormat.getBorderTop().setWidth(5);
            cellFormat.getBorderBottom().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
            cellFormat.getBorderBottom().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
            cellFormat.getBorderBottom().setWidth(5);
            cellFormat.getBorderLeft().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
            cellFormat.getBorderLeft().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
            cellFormat.getBorderLeft().setWidth(5);
            cellFormat.getBorderRight().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
            cellFormat.getBorderRight().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
            cellFormat.getBorderRight().setWidth(5);
        }
    }
    // 合并第 1 行的第 1 和第 2 个单元格
    tbl.mergeCells(tbl.getRows().get_Item(0).get_Item(0), tbl.getRows().get_Item(1).get_Item(1), false);
    // 向合并的单元格添加一些文本
    tbl.getRows().get_Item(0).get_Item(0).getTextFrame().setText("Merged Cells");
    // 将演示文稿保存到磁盘
    pres.save("table.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

标准表格中的编号

在标准表格中,单元格的编号方式简单且从零开始。表格的第一个单元格索引为 0,0(第 0 列,第 0 行)。

例如,具有 4 列 4 行的表格单元格编号如下:

(0, 0) (1, 0) (2, 0) (3, 0)
(0, 1) (1, 1) (2, 1) (3, 1)
(0, 2) (1, 2) (2, 2) (3, 2)
(0, 3) (1, 3) (2, 3) (3, 3)

以下 JavaScript 代码演示了如何为表格单元格指定编号:

// 实例化表示 PPTX 文件的 Presentation 类
var pres = new aspose.slides.Presentation();
try {
    // 访问第一张幻灯片
    var sld = pres.getSlides().get_Item(0);
    // 定义列宽和行高
    var dblCols = java.newArray("double", [70, 70, 70, 70]);
    var dblRows = java.newArray("double", [70, 70, 70, 70]);
    // 向幻灯片添加表格形状
    var tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
    // 为每个单元格设置边框格式
    for (let i = 0; i < tbl.getRows().size(); i++) {
        const row = tbl.getRows().get_Item(i);
        for (let j = 0; j < row.size(); j++) {
            const cell = row.get_Item(j);
            cell.getCellFormat().getBorderTop().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
            cell.getCellFormat().getBorderTop().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
            cell.getCellFormat().getBorderTop().setWidth(5);
            cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
            cell.getCellFormat().getBorderBottom().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
            cell.getCellFormat().getBorderBottom().setWidth(5);
            cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
            cell.getCellFormat().getBorderLeft().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
            cell.getCellFormat().getBorderLeft().setWidth(5);
            cell.getCellFormat().getBorderRight().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
            cell.getCellFormat().getBorderRight().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
            cell.getCellFormat().getBorderRight().setWidth(5);
        }
    }
    // 将演示文稿保存到磁盘
    pres.save("StandardTables_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

访问现有表格

  1. 创建一个 Presentation 类的实例。

  2. 通过索引获取包含该表格的幻灯片的引用。

  3. 创建一个 Table 对象并将其设为 null。

  4. 遍历所有 Shape 对象,直至找到表格。

    如果您怀疑当前幻灯片仅包含一个表格,可以直接检查它包含的所有形状。当形状被识别为表格时,您可以将其强制转换为 Table 对象。但如果幻灯片包含多个表格,最好通过其 setAlternativeText(String value) 方法搜索所需的表格。

  5. 使用 Table 对象对表格进行操作。在下面的示例中,我们向表格添加了一行新行。

  6. 保存修改后的演示文稿。

以下 JavaScript 代码演示了如何访问和操作现有表格:

// 实例化表示 PPTX 文件的 Presentation 类
var pres = new aspose.slides.Presentation("UpdateExistingTable.pptx");
try {
    // 访问第一张幻灯片
    var sld = pres.getSlides().get_Item(0);
    // 初始化为 null 的 TableEx
    var tbl = null;
    // 遍历形状并设置对找到的表格的引用
    for (let i = 0; i < sld.getShapes().size(); i++) {
        let shp = sld.getShapes().get_Item(i);
        if (java.instanceOf(shp, "com.aspose.slides.ITable")) {
            tbl = shp;
            // 为第二行的第一列设置文本
            tbl.get_Item(0, 1).getTextFrame().setText("New");
        }
    }
    // 将修改后的演示文稿保存到磁盘
    pres.save("table1_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

在表格中对齐文本

  1. 创建一个 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 向幻灯片添加一个 Table 对象。
  4. 从表格访问一个 TextFrame 对象。
  5. 访问该 TextFrameParagraph
  6. 垂直对齐文本。
  7. 保存修改后的演示文稿。

以下 JavaScript 代码演示了如何在表格中对齐文本:

// 创建 Presentation 类的实例
var pres = new aspose.slides.Presentation();
try {
    // 获取第一张幻灯片
    var slide = pres.getSlides().get_Item(0);
    // 定义列宽和行高
    var dblCols = java.newArray("double", [120, 120, 120, 120]);
    var dblRows = java.newArray("double", [100, 100, 100, 100]);
    // 向幻灯片添加表格形状
    var tbl = slide.getShapes().addTable(100, 50, dblCols, dblRows);
    tbl.get_Item(1, 0).getTextFrame().setText("10");
    tbl.get_Item(2, 0).getTextFrame().setText("20");
    tbl.get_Item(3, 0).getTextFrame().setText("30");
    // 访问文本框
    var txtFrame = tbl.get_Item(0, 0).getTextFrame();
    // 创建文本框的 Paragraph 对象
    var paragraph = txtFrame.getParagraphs().get_Item(0);
    // 为段落创建 Portion 对象
    var portion = paragraph.getPortions().get_Item(0);
    portion.setText("Text here");
    portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    // 垂直对齐文本
    var cell = tbl.get_Item(0, 0);
    cell.setTextAnchorType(aspose.slides.TextAnchorType.Center);
    cell.setTextVerticalType(aspose.slides.TextVerticalType.Vertical270);
    // 将演示文稿保存到磁盘
    pres.save("Vertical_Align_Text_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

在表格级别设置文本格式

  1. 创建一个 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 从幻灯片访问一个 Table 对象。
  4. 为文本设置 setFontHeight(float value)
  5. 设置 setAlignment(int value)setMarginRight(float value)
  6. 设置 setTextVerticalType(byte value)
  7. 保存修改后的演示文稿。

以下 JavaScript 代码演示了如何对表格中的文本应用首选的格式设置:

// 创建 Presentation 类的实例
var pres = new aspose.slides.Presentation("simpletable.pptx");
try {
    // 假设第一张幻灯片上的第一个形状是表格
    var someTable = pres.getSlides().get_Item(0).getShapes().get_Item(0);
    // 设置表格单元格的字体高度
    var portionFormat = new aspose.slides.PortionFormat();
    portionFormat.setFontHeight(25);
    someTable.setTextFormat(portionFormat);
    // 一次调用设置表格单元格的文本对齐方式和右侧边距
    var paragraphFormat = new aspose.slides.ParagraphFormat();
    paragraphFormat.setAlignment(aspose.slides.TextAlignment.Right);
    paragraphFormat.setMarginRight(20);
    someTable.setTextFormat(paragraphFormat);
    // 设置表格单元格的文本垂直方向类型
    var textFrameFormat = new aspose.slides.TextFrameFormat();
    textFrameFormat.setTextVerticalType(aspose.slides.TextVerticalType.Vertical);
    someTable.setTextFormat(textFrameFormat);
    pres.save("result.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

获取表格样式属性

Aspose.Slides 允许您检索表格的样式属性,以便在其他表格或其他位置使用这些信息。以下 JavaScript 代码演示了如何从表格预设样式中获取样式属性:

var pres = new aspose.slides.Presentation();
try {
    var table = pres.getSlides().get_Item(0).getShapes().addTable(10, 10, java.newArray("double", [100, 150]), java.newArray("double", [5, 5, 5]));
    table.setStylePreset(aspose.slides.TableStylePreset.DarkStyle1);// 更改默认的样式预设主题
    pres.save("table.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

锁定表格的宽高比

几何形状的宽高比是其在不同维度上的尺寸比例。Aspose.Slides 提供了 setAspectRatioLocked 属性,允许您锁定表格和其他形状的宽高比设置。

以下 JavaScript 代码演示了如何锁定表格的宽高比:

var pres = new aspose.slides.Presentation("pres.pptx");
try {
    var table = pres.getSlides().get_Item(0).getShapes().get_Item(0);
    console.log("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked());
    table.getGraphicalObjectLock().setAspectRatioLocked(!table.getGraphicalObjectLock().getAspectRatioLocked());// invert
    console.log("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked());
    pres.save("pres-out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

FAQ

我可以为整个表格及其单元格中的文本启用从右到左 (RTL) 阅读方向吗?

可以。表格提供了 setRightToLeft 方法,段落具有 ParagraphFormat.setRightToLeft。两者结合使用可确保单元格内的 RTL 顺序和渲染正确。

如何防止用户在最终文件中移动或调整表格大小?

使用 shape locks 来禁用移动、调整大小、选择等。这些锁定同样适用于表格。

是否支持在单元格内插入图像作为背景?

可以。您可以为单元格设置 picture fill,图像将根据所选模式(拉伸或平铺)覆盖单元格区域。