我用apache POI创建了一个PPT演示文稿,我想用下面的代码为PPT添加Title。但它会抛出编译错误
表达式的类型必须是数组类型,但它已解析为List
let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(MapViewController.postDelayTimer), userInfo: nil, repeats: false)
myTimer.fire()
答案 0 :(得分:1)
这里的问题是,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);
}
}
}