java删除表操作_Java 创建、删除、操作PPT中的表格

常言道,文不如图,图不如表。因此在制作PPT过程中,利用表格将有力的数据表示出来,不仅可以提升整个PPT的质量,同时也能够向观众直观地阐述观点,更具说服力。本文将通过使用Java程序来演示如何在PPT中创建、删除和操作表格。

Jar文件获取及导入:

方法1:通过官方网站下载获取jar包。解压后将lib文件夹下的Spire.Presentation.jar文件导入Java程序。(如下图)

7769402f9aed

方法2:通过maven仓库安装导入。具体安装教程详见此网页。

【示例1】创建表格

importcom.spire.presentation.FileFormat;

importcom.spire.presentation.ITable;

importcom.spire.presentation.Presentation;

importcom.spire.presentation.TableStylePreset;

importcom.spire.presentation.TextAlignmentType;

public classAddTable {

publicstatic void main(String[] args) throwsException {

//实例化一个Presentation对象

Presentation presentation = newPresentation();

//设置表格行数和列数、行高和列宽

Double[] widths = newDouble[] { 80d, 80d, 100d, 80d };

Double[] heights =newDouble[] { 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(inti = 0; i < 4; i++)

{table.get(i,0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);

}

//声明一个String数组

String[][] dataStr = newString[][]

{{"姓名", "性别", "部门", "工号"},

{"Winny", "女", "综合", "0109"},

{"Lois", "女", "综合", "0111"},

{"Jois", "男", "技术", "0110"},

{"Moon", "女", "销售", "0112"},

{"Winny", "女", "后勤", "0113"}

};

//向表格中填充数据

for(inti = 0; i < 6; i++)

{for(intj = 0; j < 4; j++)

{table.get(j,i).getTextFrame().setText(dataStr[i][j]);

}

}

//保存文件

presentation.saveToFile("output/CreateTable.pptx", FileFormat.PPTX_2013);

}

}

表格创建效果:

7769402f9aed

【示例2】删除行和列

importcom.spire.presentation.*;

public classDeleteTableRoWAndColumn {

public static voidmain(String[] args) throwsException {

//实例化一个ppt对象并加载示例文档

Presentation ppt = newPresentation();

ppt.loadFromFile("D:\\Desktop\\CreateTable.pptx");

//获取第一张幻灯片上的表格

ISlide slide = ppt.getSlides().get(0);

ITable table =null;

for(inti = 0; i < slide.getShapes().getCount(); i++) {

IShape shape = slide.getShapes().get(i);

if((shape instanceofITable)) {

table = ((ITable) (shape));

//删除列

table.getColumnsList().removeAt(2, false);

//删除行

table.getTableRows().removeAt(3, false);

}

}

//保存文档

ppt.saveToFile("output/DeleteRowAndColumn.pptx", FileFormat.PPTX_2013)

}

}

行和列删除效果:

7769402f9aed

【示例3】合并单元格

importcom.spire.presentation.FileFormat;

importcom.spire.presentation.ITable;

importcom.spire.presentation.Presentation;

public classMergeCells {

public static voidmain(String[] args) throwsException {

//实例化一个PPT对象

Presentation presentation = newPresentation();

//加载PowerPoint文档

presentation.loadFromFile("D:\\Desktop\\CreateTable.pptx");

//声明ITable变量

ITable table = null;

//获取PPT中的表格

for(Object shape : presentation.getSlides().get(0).getShapes()) {

if(shape instanceofITable) {table = (ITable) shape;

//合并单元格

table.mergeCells(table.get(0, 1), table.get(0, 3), false);

}

}

//保存文档

presentation.saveToFile("output/MergeTableCells.pptx", FileFormat.PPTX_2010);

}

}

单元格合并效果:

7769402f9aed

【示例4】删除表格

importcom.spire.presentation.FileFormat;

importcom.spire.presentation.Presentation;

public classDeleteTable {

public static voidmain(String[] args) throwsException {

//实例化一个ppt对象

Presentation ppt = newPresentation();

//加载PowerPoint文档

ppt.loadFromFile("D:\\Desktop\\CreateTable.pptx");

//删除第一张幻灯片中的第一个表格

ppt.getSlides().get(0).getShapes().removeAt(0);

//保存文档

ppt.saveToFile("output/DeleteTable.pptx", FileFormat.PPTX_2013);

}

}

表格删除效果:

7769402f9aed

(本文完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值