Java|Java 创建、删除、操作PPT中的表格

常言道,文不如图,图不如表。因此在制作PPT过程中,利用表格将有力的数据表示出来,不仅可以提升整个PPT的质量,同时也能够向观众直观地阐述观点,更具说服力。本文将通过使用Java程序来演示如何在PPT中创建、删除和操作表格。
使用工具:Free Spire.Presentation for Java(免费版)
Jar文件获取及导入: 方法1通过官方网站下载获取jar包。解压后将lib文件夹下的Spire.Presentation.jar文件导入Java程序。(如下图)


Java|Java 创建、删除、操作PPT中的表格
文章图片
方法2通过maven仓库安装导入。具体安装教程详见此网页。
【示例1】创建表格 import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
import com.spire.presentation.TableStylePreset;
import com.spire.presentation.TextAlignmentType;
public class AddTable {
public static void main(String[] args) throws Exception {
//实例化一个Presentation对象
Presentation presentation = new Presentation();
//设置表格行数和列数、行高和列宽
Double[] widths = new Double[] { 80d, 80d, 100d, 80d };
Double[] heights =new Double[] { 15d, 15d, 15d, 15d, 15d, 15d };
//添加一个表格
ITable table = presentation.getSlides().get(0).getShapes().appendTable((float)presentation.getSlideSize().
getSize().getWidth() /2 - 275, 90, widths, heights);
//设置表格内置样式
table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);
//设置对齐方式
for (int i = 0; i < 4; i++)
{table.get(i,0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
}
//声明一个String数组
String[][] dataStr = new String[][]
{{"姓名", "性别", "部门", "工号"},
{"Winny", "女", "综合", "0109"},
{"Lois", "女", "综合", "0111"},
{"Jois", "男", "技术", "0110"},
{"Moon", "女", "销售", "0112"},
{"Winny", "女", "后勤", "0113"}
};
//向表格中填充数据
for (int i = 0; i < 6; i++)
{for (int j = 0; j < 4; j++)
{table.get(j,i).getTextFrame().setText(dataStr[i][j]);
}
}
//保存文件
presentation.saveToFile("output/CreateTable.pptx", FileFormat.PPTX_2013);
}
}
表格创建效果:

Java|Java 创建、删除、操作PPT中的表格
文章图片

【示例2】删除行和列 import com.spire.presentation.*;
public class DeleteTableRoWAndColumn {
public static void main(String[] args) throws Exception {
//实例化一个ppt对象并加载示例文档
Presentation ppt = new Presentation();
ppt.loadFromFile("D:\\Desktop\\CreateTable.pptx");
//获取第一张幻灯片上的表格
ISlide slide = ppt.getSlides().get(0);
ITable table =null;
for (int i = 0; i < slide.getShapes().getCount(); i++) {
IShape shape = slide.getShapes().get(i);
if ((shape instanceof ITable)) {
table = ((ITable) (shape));
//删除列
table.getColumnsList().removeAt(2, false);
//删除行
table.getTableRows().removeAt(3, false);
}
}
//保存文档
ppt.saveToFile("output/DeleteRowAndColumn.pptx", FileFormat.PPTX_2013)
}
}
行和列删除效果:

Java|Java 创建、删除、操作PPT中的表格
文章图片

【示例3】合并单元格 import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
public class MergeCells {
public static void main(String[] args) throws Exception {
//实例化一个PPT对象
Presentation presentation = new Presentation();
//加载PowerPoint文档
presentation.loadFromFile("D:\\Desktop\\CreateTable.pptx");
//声明ITable变量
ITable table = null;
//获取PPT中的表格
for (Object shape : presentation.getSlides().get(0).getShapes()) {
if (shape instanceof ITable) {table = (ITable) shape;
//合并单元格
table.mergeCells(table.get(0, 1), table.get(0, 3), false);
}
}
//保存文档
presentation.saveToFile("output/MergeTableCells.pptx", FileFormat.PPTX_2010);
}
}
单元格合并效果:

Java|Java 创建、删除、操作PPT中的表格
文章图片

【示例4】删除表格 import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class DeleteTable {
public static void main(String[] args) throws Exception {
//实例化一个ppt对象
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.loadFromFile("D:\\Desktop\\CreateTable.pptx");
//删除第一张幻灯片中的第一个表格
ppt.getSlides().get(0).getShapes().removeAt(0);
//保存文档
ppt.saveToFile("output/DeleteTable.pptx", FileFormat.PPTX_2013);
}
}
表格删除效果:

Java|Java 创建、删除、操作PPT中的表格
文章图片
【Java|Java 创建、删除、操作PPT中的表格】(本文完)

    推荐阅读