Free Spire.Presentation for Java (免费版)
IntelliJ IDEA
方法1:通过官网下载jar文件。下载文件并解压,将lib文件夹下的Spire.Presentation.jar文件导入到Java程序。
方法2:可通过maven仓库安装导入到maven项目。
import com.spire.presentation.*; import java.awt.*; public class AddTable { public static void main(String[]args) throws Exception { //实例化一个Presentation对象 Presentation presentation = new Presentation(); //设置表格行数和列数、行高和列宽 Double[] widths = new Double[] { 100d, 100d, 100d, 100d,100d }; 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_1_ACCENT_6); //声明一个String数组 String[][] dataStr = new String[][] { {'班级', '姓名', '性别', '学号','总分'}, {'1班', '李龙飞', '男', 'Y010956','658'}, {'1班', '阜南', '男', 'Y011561','686'}, {'5班', '陈琳', '女', 'Y011650','654'}, {'9班', '房芬', '女', 'Y011258','638'}, {'13班', '张立', '男', 'Y011328','645'} }; //向表格中填充数据 for (int i = 0; i < 6; i++) { for (int j = 0; j < 5; j++) { table.get(j, i).getTextFrame().setText(dataStr[i][j]); table.get(j, i).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER); } } //合并单元格 table.mergeCells(table.get(0, 2), table.get(0,2 ), false); //设置表格边框样式 table.setTableBorder(TableBorderType.Inside_Horizontal, 1, Color.black); //保存文件 presentation.saveToFile('添加表格.pptx', FileFormat.PPTX_2013); } }
表格添加效果: