gpt4 book ai didi

java - Poi 幻灯片格式

转载 作者:行者123 更新时间:2023-11-30 07:15:00 24 4
gpt4 key购买 nike

我已经使用 apache POI 创建了一个 PPT 演示文稿,我想使用以下代码为 PPT 添加标题。但它会抛出编译错误

The type of the expression must be an array type but it resolved to List

public static void main(String args[]) throws IOException{

//creating presentation
XMLSlideShow ppt = new XMLSlideShow();

//getting the slide master object
XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];

//get the desired slide layout
XSLFSlideLayout titleLayout = slideMaster.getLayout(SlideLayout.TITLE);

//creating a slide with title layout
XSLFSlide slide1 = ppt.createSlide(titleLayout);

//selecting the place holder in it
XSLFTextShape title1 = slide1.getPlaceholder(0);

最佳答案

这里的问题是 ppt.getSlideMasters()返回List<XSLFSlideMaster>而不是XSLFSlideMaster[]正如你所期待的。所以,对于你要解决的问题,下面的代码应该就可以了:

import org.apache.poi.xslf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;


public class Slideshow {
public static void main(String[] args) throws IOException {
//creating presentation
try (FileOutputStream out = new FileOutputStream("example.ppt");
XMLSlideShow ppt = new XMLSlideShow();) {
//getting the slide master object
XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0);

//get the desired slide layout
XSLFSlideLayout titleLayout = slideMaster.getLayout(SlideLayout.TITLE);

//creating a slide with title layout
XSLFSlide slide1 = ppt.createSlide(titleLayout);

//selecting the place holder in it
XSLFTextShape title1 = slide1.getPlaceholder(0);
title1.setText("Text title");
ppt.write(out);
}
}
}

结果将是: enter image description here

关于java - Poi 幻灯片格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38560723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com