管理 Java 中的演示文稿表格

简介

PowerPoint 中的表格将信息组织为行和列,使阅读和比较数值更容易。

Aspose.Slides 提供了 Table 类、ITable 接口、Cell 类、ICell 接口以及其他类型,帮助您在演示文稿中创建、更新和管理表格。

从头创建表格

通过指定位置、列宽和行高来创建表格。将其添加到幻灯片后,您可以设置单元格边框、合并单元格并插入文本。

  1. 创建 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 定义以点为单位的列宽数组。
  4. 定义以点为单位的行高数组。
  5. 通过 addTable 方法向幻灯片添加 ITable 对象。
  6. 遍历每个 ICell ,为上、下、右、左边框应用格式。
  7. 合并表格第一行的前两个单元格。
  8. 通过其 getTextFrame 方法访问合并后的单元格。
  9. 设置合并单元格中的文本。
  10. 保存修改后的演示文稿。

下面的示例在 (100, 50) 点处创建了一个包含三列五行的表格。它使用宽度为 5 点的红色边框,合并第一行的前两个单元格,并将结果保存为 table.pptx。

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    double[] columnWidths = { 50, 50, 50 };
    double[] rowHeights = { 50, 30, 30, 30, 30 };
    ITable table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);

    for (IRow row : table.getRows())
    {
        for (ICell cell : row)
        {
            ICellFormat cellFormat = cell.getCellFormat();
            cellFormat.getBorderTop().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderTop().setWidth(5);

            cellFormat.getBorderBottom().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderBottom().setWidth(5);

            cellFormat.getBorderLeft().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderLeft().setWidth(5);

            cellFormat.getBorderRight().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderRight().setWidth(5);
        }
    }

    table.mergeCells(table.get_Item(0, 0), table.get_Item(1, 0), false);
    table.get_Item(0, 0).getTextFrame().setText("Merged Cells");

    presentation.save("table.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

标准表格的编号

在标准表格中,单元格索引从零开始,顺序为(列,行)。第一个单元格的索引为 (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)

此示例创建了上图所示的 4 × 4 表格,列宽和行高均为 70 点,单元格边框为宽度为 5 点的红色。坐标显示单元格索引;示例保持单元格为空,并将表格保存为 StandardTables_out.pptx。

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    double[] columnWidths = { 70, 70, 70, 70 };
    double[] rowHeights = { 70, 70, 70, 70 };
    ITable table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);

    for (IRow row : table.getRows())
    {
        for (ICell cell : row)
        {
            ICellFormat cellFormat = cell.getCellFormat();
            cellFormat.getBorderTop().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderTop().setWidth(5);

            cellFormat.getBorderBottom().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderBottom().setWidth(5);

            cellFormat.getBorderLeft().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderLeft().setWidth(5);

            cellFormat.getBorderRight().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderRight().setWidth(5);
        }
    }

    presentation.save("StandardTables_out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

访问现有表格

表格存储在幻灯片的形状集合中。遍历形状以定位表格,然后使用 ITable 接口读取或更新其单元格。

  1. 使用 Presentation 类加载演示文稿。
  2. 通过索引获取包含该表格的幻灯片的引用。
  3. 遍历 IShape 对象,找到表格后停止。如果幻灯片包含多个表格,使用 getAlternativeText 来识别所需的表格。
  4. 更新目标单元格中的文本。
  5. 保存修改后的演示文稿。

下面的示例打开 UpdateExistingTable.pptx 并在第一张幻灯片上找到第一个表格。它将第 0 列第 1 行的单元格设置为 New,并将结果保存为 table1_out.pptx。输入必须至少包含一张幻灯片,且该幻灯片上的第一个表格必须至少有一列两行。

import com.aspose.slides.*;

Presentation presentation = new Presentation("UpdateExistingTable.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ITable table = null;

    for (IShape shape : slide.getShapes()) {
        if (shape instanceof ITable) {
            table = (ITable) shape;
            break;
        }
    }

    if (table != null) {
        table.get_Item(0, 1).getTextFrame().setText("New");
        presentation.save("table1_out.pptx", SaveFormat.Pptx);
    }
} finally {
    presentation.dispose();
}

要调整现有表格中行的大小并了解其实际高度为何可能超过请求的最小值,请参阅 Control Row Height。

查找拥有文本帧的单元格

当通用文本处理代码从表格中获取 ITextFrame 时,使用 ITextFrame.getParentCell 方法检索拥有该框的 ICell。对于表格单元格的文本帧,ITextFrame.getParentCell 返回所有者,而 ITextFrame.getParentShape 返回 null,即使表格本身是一个形状。

单元格坐标可通过只读的 ICell.getFirstColumnIndex 和 ICell.getFirstRowIndex 方法获取。ITextFrame.getParentCell 也提供只读的导航:它返回所有者但不更改所有权。在使用返回的单元格之前,请始终检查其是否为 null。

要查看完整的示例(识别表格单元格和形状所有者,包括与 SmartArt 节点关联的形状),请参阅 Search and Replace Text。

对齐表格中的文本

您可以控制单个表格单元格的垂直锚定和文本方向。本节示例将第一单元格中的文本居中并旋转 270 度。

  1. 创建 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 向幻灯片添加 ITable 对象。
  4. 从表格获取 ITextFrame 对象。
  5. 访问第一个 IParagraph ,并设置其文本和颜色。
  6. 使用 setTextAnchorType 和 setTextVerticalType 设置单元格的垂直锚定和文本方向。
  7. 保存修改后的演示文稿。

此示例创建了一个 4 × 4 表格,列宽为 120 点,行高为 100 点。它对单元格 (0, 0) 的文本进行格式化,在第一行的其余单元格中添加数值,并将结果保存为 Vertical_Align_Text_out.pptx。

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    double[] columnWidths = { 120, 120, 120, 120 };
    double[] rowHeights = { 100, 100, 100, 100 };
    ITable table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);

    table.get_Item(1, 0).getTextFrame().setText("10");
    table.get_Item(2, 0).getTextFrame().setText("20");
    table.get_Item(3, 0).getTextFrame().setText("30");

    ITextFrame textFrame = table.get_Item(0, 0).getTextFrame();
    IParagraph paragraph = textFrame.getParagraphs().get_Item(0);

    IPortion portion = paragraph.getPortions().get_Item(0);
    portion.setText("Text here");
    portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);

    ICell cell = table.get_Item(0, 0);
    cell.setTextAnchorType(TextAnchorType.Center);
    cell.setTextVerticalType(TextVerticalType.Vertical270);

    presentation.save("Vertical_Align_Text_out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

在表格级别设置文本格式

使用 setTextFormat 将文本格式应用于表格中的所有单元格。其重载接受部分、段落和文本框的格式设置,从而无需遍历各个单元格即可设置这些属性。

  1. 使用 Presentation 类加载演示文稿。
  2. 通过索引获取幻灯片的引用。
  3. 从幻灯片获取 ITable 对象。
  4. 使用 setFontHeight 为文本设置字体大小。
  5. 使用 setAlignment 和 setMarginRight 设置段落对齐方式和右边距。
  6. 使用 setTextVerticalType 设置文本方向。
  7. 保存修改后的演示文稿。

下面的示例打开 table.pptx(该文件必须至少包含一张幻灯片,且该幻灯片的第一个形状为表格)。它将字体大小设置为 25 点,将段落右对齐并设置右边距为 20 点,并将文本设为垂直。格式化后的演示文稿保存为 result.pptx。

import com.aspose.slides.*;

Presentation presentation = new Presentation("table.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ITable table = (ITable) slide.getShapes().get_Item(0);

    PortionFormat portionFormat = new PortionFormat();
    portionFormat.setFontHeight(25);
    table.setTextFormat(portionFormat);

    ParagraphFormat paragraphFormat = new ParagraphFormat();
    paragraphFormat.setAlignment(TextAlignment.Right);
    paragraphFormat.setMarginRight(20);
    table.setTextFormat(paragraphFormat);

    TextFrameFormat textFrameFormat = new TextFrameFormat();
    textFrameFormat.setTextVerticalType(TextVerticalType.Vertical);
    table.setTextFormat(textFrameFormat);

    presentation.save("result.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

获取表格样式属性

使用 getStylePreset 读取表格的预设样式,使用 setStylePreset 进行分配。此示例将 TableStylePreset.DarkStyle1 应用于一个表格,打印预设值,并将相同的预设分配给第二个表格。两个表格均保存在 table-style.pptx 中。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    double[] columnWidths = { 100, 150 };
    double[] rowHeights = { 5, 5, 5 };
    ITable table = slide.getShapes().addTable(10, 10, columnWidths, rowHeights);
    table.setStylePreset(TableStylePreset.DarkStyle1);

    int stylePreset = table.getStylePreset();
    System.out.println("Table style preset: " + stylePreset);

    ITable anotherTable = slide.getShapes().addTable(10, 100, columnWidths, rowHeights);
    anotherTable.setStylePreset(stylePreset);

    presentation.save("table-style.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

锁定表格的宽高比

表格的宽高比是其宽度与高度的比例。使用 setAspectRatioLocked 可锁定表格的此比例。

下面的示例打开 pres.pptx(该文件必须至少包含一张幻灯片,且该幻灯片的第一个形状为表格)。它打印当前的锁定状态,启用宽高比锁定,打印更新后的状态(true),并将结果保存为 pres-out.pptx。

import com.aspose.slides.*;

Presentation presentation = new Presentation("pres.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    ITable table = (ITable) slide.getShapes().get_Item(0);
    System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked());

    table.getGraphicalObjectLock().setAspectRatioLocked(true);
    System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked());

    presentation.save("pres-out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

FAQ

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

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

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

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

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

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