Java生成和操作PowerPoint表格

用表格展示数据比单纯的文字更加清晰明了,因此在PPT演示文稿中经常会出现表格。这篇文章将介绍如何使用免费PowerPoint API - Free Spire.Presentation for Java在Java应用程序中生成和操作PowerPoint表格。

导入jar文件

在开始前,我们需要导入jar文件。下载Free Spire.Presentation for Java并解压缩,然后从lib文件夹下,导入jar包到你的Java应用程序中。

代码示例

1. 生成表格

import com.spire.presentation.*;

public class AddTable {
    public static void main(String[] args) throws Exception {
        //创建PowerPoint文档
        Presentation presentation = new Presentation();

        Double[] widths = new Double[]{150d, 100d, 100d, 100d, 100d};
        Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d};

        //添加一个表格到第一张幻灯片
        ITable table = presentation.getSlides().get(0).getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);


        String[][] dataStr = new String[][]
                {
                        {"公司", "一月", "二月", "三月", "合计"},
                        {"成都分公司", "7", "7", "5", "19"},
                        {"上海分公司", "6", "4", "7", "17"},
                        {"武汉分公司", "8", "7", "9", "24"},
                        {"总计", "21", "18", "21", "60"},
                };


        //添加数据到表格
        for (int i = 0; i < 5; 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).getTextRanges().get(0).setLatinFont(new TextFont("宋体"));
                table.get(j, i).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
            }
        }

        //设置表格的样式
        table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);

        //保存文档
        presentation.saveToFile("AddTable.pptx", FileFormat.PPTX_2013);
    }
}

2. 操作表格

要操作现有表格,首先需要获取这个table对象:

//加载PowerPoint文档
Presentation ppt = new Presentation();
ppt.loadFromFile("AddTable.pptx");

//获取table对象
ITable table = (ITable) ppt.getSlides().get(0).getShapes().get(0);

部分常规操作: 

添加行、列 

table.getTableRows().append(table.getTableRows().get(0));
table.getColumnsList().add(table.getColumnsList().get(0));

插入行、列 

table.getTableRows().insert(0, table.getTableRows().get(0));
table.getColumnsList().insert(0, table.getColumnsList().get(0));

设置行高、列宽 

table.getTableRows().get(0).setHeight(50);
table.getColumnsList().get(0).setWidth(100);

删除行、列 

table.getTableRows().removeAt(0, false);
table.getColumnsList().removeAt(0, false);

合并单元格 

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

拆分单元格 

table.get(0,0).Split(3,2);

单元格填充图片 

table.get(0,0).getFillFormat().setFillType(FillFormatType.PICTURE);
table.get(0,0).getFillFormat().getPictureFill().getPicture().setUrl((new java.io.File("bkg.jpg")).getAbsolutePath());

单元格填充颜色 

table.get(0,1).getFillFormat().setFillType(FillFormatType.SOLID);
table.get(0,1).getFillFormat().getSolidColor().setColor(Color.blue);

表格边框 

table.setTableBorder(TableBorderType.All, 1, Color.black);

操作完成后,别忘记保存文档: 

ppt.saveToFile("ManipulateTable.pptx", FileFormat.PPTX_2013);

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值