文本介绍如何使用spire.presentation for java在现有的ppt表格中添加或删除行和列。
测试文档:
添加行和列
import com.spire.presentation.*;
public class addrowandcolumn {
public static void main(string[] args) throws exception {
//加载示例文档
presentation presentation = new presentation();
presentation.loadfromfile("c:\\users\\administrator\\desktop\\input.pptx");
//获取表格
itable table = null;
for (object shape : presentation.getslides().get(0).getshapes()) {
if (shape instanceof itable) {
table = (itable) shape;
//将最后一行作为新行添加到表格下方
int rowcount = table.gettablerows().getcount();
tablerow row = table.gettablerows().get(rowcount - 1);
table.gettablerows().append(row);
//获取新行,并设置每个单元格的文本
rowcount = table.gettablerows().getcount();
row = table.gettablerows().get(rowcount - 1);
row.get(0).gettextframe().settext("美国");
row.get(1).gettextframe().settext("华盛顿");
row.get(2).gettextframe().settext("北美洲");
row.get(3).gettextframe().settext("9372610");
//将最后一列作为新列添加到表格右方
int colcount = table.getcolumnslist().getcount();
tablecolumn column =table.getcolumnslist().get(colcount-1);
table.getcolumnslist().add(column);
//获取新列,并设置每个单元格的文本
colcount = table.getcolumnslist().getcount();
column = table.getcolumnslist().get(colcount-1);
column.get(0).gettextframe().settext("人口");
column.get(1).gettextframe().settext("32370000");
column.get(2).gettextframe().settext("7350000");
column.get(3).gettextframe().settext("15040000");
column.get(4).gettextframe().settext("26500000");
column.get(5).gettextframe().settext("329740000");
}
}
//保存文档
presentation.savetofile("output/addrowandcolumn.pptx", fileformat.pptx_2013);
}
}
删除行和列
import com.spire.presentation.fileformat;
import com.spire.presentation.itable;
import com.spire.presentation.presentation;
public class deleterowandcolumn {
public static void main(string[] args) throws exception {
//加载示例文档
presentation presentation = new presentation();
presentation.loadfromfile("c:\\users\\administrator\\desktop\\input.pptx");
//获取表格
itable table = null;
for (object shape : presentation.getslides().get(0).getshapes()) {
if (shape instanceof itable) {
table = (itable) shape;
//删除第二列
table.getcolumnslist().removeat(1, false);
//删除第二行
table.gettablerows().removeat(1, false);
}
}
//保存文档
presentation.savetofile("output/deleterowandcolumn.pptx", fileformat.pptx_2013);
}
}