常言道,文不如图,图不如表。所以在制做PPT过程当中,利用表格将有力的数据表示出来,不只能够提高整个PPT的质量,同时也可以向观众直观地阐述观点,更具说服力。本文将经过使用Java程序来演示如何在PPT中建立、删除和操做表格。html
方法1:经过官方网站下载获取jar包。解压后将lib文件夹下的Spire.Presentation.jar文件导入Java程序。(以下图)java
方法2:经过maven仓库安装导入。具体安装教程详见此网页。数组
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); } }
表格建立效果:app
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); } }
行和列删除效果:maven
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 { //实例化一个Presentation对象 Presentation presentation = new Presentation(); //加载示例文档 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); } }
单元格合并效果:ide
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); } }
表格删除效果:工具
(本文完)网站