如何使用 Java 在幻灯片中插入表格

这个简短的教程将指导您如何使用 Java 在幻灯片中插入表格。它分享了设置 IDE 以编写应用程序所需的详细信息以及执行此任务的详细步骤。最后,您将获得一个可运行的示例代码,演示如何使用 Java 在 PowerPoint 中制作表格并将生成的演示文稿保存在磁盘上为 PPTXPPT 或任何其他格式支持的格式,无需安装 MS PowerPoint 或任何其他第三方工具。

使用 Java 在 PowerPoint 中添加表格的步骤

  1. 将 IDE 设置为使用 Aspose.Slides 插入表格
  2. 使用 Presentation 类创建一个新的演示文稿并访问第一张幻灯片
  3. 使用 addTable() 方法并提供文本位置和单元格的高度和宽度,在所选幻灯片中添加新表格
  4. 遍历所有行并设置文本以及字体设置
  5. 将演示文稿保存在包含表格的磁盘上

上述步骤解释了如何使用 Java 在 PowerPoint 中创建表格。确定了完成任务所需的所有必要类和方法。这个过程很简单,将表格添加到幻灯片,然后在其单元格中填充一些示例文本。

使用 Java 在 PowerPoint 中创建表格的代码

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testslides;
import com.aspose.slides.BulletType;
import com.aspose.slides.ICell;
import com.aspose.slides.IRow;
import com.aspose.slides.ISlide;
import com.aspose.slides.ITable;
import com.aspose.slides.ITextFrame;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
public class AddTable {
public static void main(String[] args) throws Exception {//handle exception during adding any tablle
String path = "/Users/mudassirkhan/Documents/KnowledgeBase/TestData/";
// Load the product license to create a table
License slidesTablelic = new License();
slidesTablelic.setLicense(path + "Conholdate.Total.Product.Family.lic");
// Create a new presentation
Presentation presentationForTable = new Presentation();
// Access the first slide in the presentation
ISlide slide = presentationForTable.getSlides().get_Item(0);
// Define the arrays containing column widths and row heights
double[] dColumnsWidths = { 55, 55, 55 };
double[] dRowsHeights = { 55, 36, 36, 36, 36 };
// Add a new table with desired rows and columns
ITable newTable = slide.getShapes().addTable(60, 60, dColumnsWidths, dRowsHeights);
// Traverse through all the rows
for (IRow tableRow : newTable.getRows())
{
// Traverse through all the cells in the current row
for (ICell tableCell : tableRow)
{
// Access the respective text frame of the cell
ITextFrame txtFormat = tableCell.getTextFrame();
// Add some text to the cell
int iFirstRowIndex = tableCell.getFirstRowIndex();
int iFirstColumnIndex = tableCell.getFirstColumnIndex();
txtFormat.setText( "Text " + iFirstRowIndex + " " + iFirstColumnIndex);
// Set the font related property for the newly added text
txtFormat.getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().setFontHeight(10);
txtFormat.getParagraphs().get_Item(0).getParagraphFormat().getBullet().setType(BulletType.None);
}
}
// Save the presentation with table
presentationForTable.save("PresentationTable.pptx", com.aspose.slides.SaveFormat.Pptx);
System.out.println("Done");
}
}

此代码演示如何使用 Java 在 PowerPoint 中创建表格。多个接口类用于使用不同的功能,例如 ISlide 用于访问幻灯片,ITable 类用于处理表格,IRow 和 ICell 类用于处理单个单元格。 ITextFrame 类是用于设置单元格中的文本和设置单元格格式的主要接口。

本教程教会了我们如何使用 Java 在 PowerPoint 演示文稿中制作表格。如果您想了解从演示文稿创建 HTML 的过程,请参阅 如何使用 Java 在 HTML 中创建 PowerPoint 幻灯片 上的文章。

 简体中文