Java 建立、刪除、操做PPT中的表格

常言道,文不如圖,圖不如表。所以在製做PPT過程當中,利用表格將有力的數據表示出來,不只能夠提高整個PPT的質量,同時也可以向觀衆直觀地闡述觀點,更具說服力。本文將經過使用Java程序來演示如何在PPT中建立、刪除和操做表格。html

使用工具:Free Spire.Presentation for Java(免費版)

Jar文件獲取及導入:

方法1:經過官方網站下載獲取jar包。解壓後將lib文件夾下的Spire.Presentation.jar文件導入Java程序。(以下圖)
image001.jpgjava

方法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);

    }

}

表格建立效果:app

image002.jpg

【示例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);

    }
}

行和列刪除效果:maven

image003.jpg

【示例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 {
        //實例化一個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

image004.jpg

【示例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);
    }
}

表格刪除效果:工具

image005.jpg

(本文完)網站

相關文章
相關標籤/搜索