Java使用poi操作ppt

Java使用poi操作ppt

https://editor.csdn.net/md/?articleId=117926694
上一篇中写了操作文本框和插入图片

这一篇主要是如何在有模板的情况下如替换文本和修改表格

替换表示符号文本
1. 加载模板到对象中
// 模板设置到项目中resources目录下ppt文件夹
ClassPathResource classPathResource = new ClassPathResource("ppt/template.pptx");
InputStream inputStream = classPathResource.getInputStream();
// 设置到poi框架中的对象中XMLSlideShow
XMLSlideShow ppt = new XMLSlideShow(inputStream);
2.替换文本
// 获取模板中的每一页的ppt
List<XSLFSlide> slides = ppt.getSlides();
// 遍历操作
for (XSLFShape shape : shapes) {
    if (shape instanceof AutoShape) {
       		AutoShape autoShape = (AutoShape) shape;
            String text = autoShape.getText();
            if (text.contains("{YEAR}")) {
                	TextRun textRun = autoShape.setText(text.replace("{YEAR}", year));
                    // 设置文本格式 
                	textRun.setFontFamily("黑体");
                	textRun.setFontSize(36.0);
                	textRun.setFontColor(Color.RED);
                 }
             }
}
替换表格

表格我直接在模板中设置好了表头

// 获取模板中的每一页的ppt
List<XSLFSlide> slides = ppt.getSlides();
// 遍历操作
for (XSLFShape shape : shapes) {
     if (xslfShape instanceof XSLFTable) {
         // 获取ppt中表格对象
         XSLFTable xslfTable = (XSLFTable) xslfShape;
         // 新建一行
         XSLFTableRow tableRow = xslfTable.addRow();
         XSLFTableCell tableCell1 = tableRow.addCell();
         XSLFTextParagraph textParagraph1 = tableCell1.addNewTextParagraph();
         XSLFTextRun textRun = textParagraph1.addNewTextRun();
         textRun.setText("内容1");
         // 文本对齐
         textParagraph1.setTextAlign(TextParagraph.TextAlign.CENTER);
         // 设置文本样式
         setTextRunStyle(textRun);
         // 第二个单元格
         XSLFTableCell tableCell2 = tableRow.addCell();
        // 设置单元格格式
         setTableCellStyle(tableCell2);
         
         
        // 数据第5行的第1 2的单元格合并 参数firstrow lastrow firstCol lastCol
        xslfTable.mergeCells(4, 4, 0, 1);
     }
}
/**
     * @Description: 设置字体样式
     * @Author: zhy
     * @Date: 2021/6/22 11:30
     * @Param: [textRun]
     * @return: void
     */
    private void setTextRunStyle(XSLFTextRun textRun) {
        textRun.setFontSize(12.0);
        // 设置黑体
        textRun.setBold(false);
        // 设置斜体
        textRun.setItalic(false);
        // 设置字体
        textRun.setFontFamily("宋体");
        textRun.setFontColor(Color.black);
    }

 /**
     * @Description: 设置单元格的样式
     * @Author: zhy
     * @Date: 2021/6/24 16:55
     * @Param:  [tableCell]
     * @return: void
     */
    private void setTableCellStyle(XSLFTableCell tableCell) {
        // 是背景色吗
        tableCell.setFillColor(Color.GRAY);
        // 垂直对齐方式
        tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
        tableCell.setLeftInset(1);
        tableCell.setRightInset(1);
        tableCell.setBottomInset(1);
        tableCell.setTopInset(1);
        // 设置边框的颜色
        tableCell.setBorderColor(TableCell.BorderEdge.bottom, Color.BLACK);
        tableCell.setBorderColor(TableCell.BorderEdge.top, Color.BLACK);
        tableCell.setBorderColor(TableCell.BorderEdge.left, Color.BLACK);
        tableCell.setBorderColor(TableCell.BorderEdge.right, Color.BLACK);
    }

我这个是因为项目中,需要将一个excel表中导入ppt中,但是没有办法直接导入,所以在ppt中新建一个表格的表头,然后数据查询出来之后,再一行一行的填充。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值