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


  表达式的类型必须是数组类型,但解析为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);
        }
    }
}


结果将是:
java - POI幻灯片格式-LMLPHP

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

10-09 03:49