理解:ppt与Excel不同的是 ppt每页都是一个个模块组成,不同的模块有不同的方法处理。
比如:文本框、table表格、图表、背景等;
1.需要的依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.24</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
2.根据ppt模板导出ppt
public void exportPpt(HttpServletResponse response, VehPerform vehPerform){
//获取ppt模板 这里是模板的路径
String url = System.getProperty("user.dir") + "//vpm-modules//vpm-business//src//main//resources//file//templatePPt.pptx";
try (InputStream inputStream = new FileInputStream(url)) {
// 设置最小解压比率以解决 Zip bomb 错误
ZipSecureFile.setMinInflateRatio(-1.0d);
//获取ppt
XMLSlideShow ppt = new XMLSlideShow(inputStream);
//获取ppt所有页 数据
List<XSLFSlide> slides = ppt.getSlides();
//第一页 文本框数据处理
one(slides.get(0));
//第四页 table表格数据处理
four(slides.get(3));
/*导出到本地的路径
FileOutputStream outputStream = new FileOutputStream("路径");
ppt.write(outputStream);
outputStream.close();
ppt.close();*/
response.setContentType("application/vnd.ms-powerpoint;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + "");
response.setCharacterEncoding("UTF-8");
OutputStream os = response.getOutputStream();
ppt.write(os);
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//第一页 文本框数据处理
public static void one(XSLFSlide slide,String code,String name){
//获取所有模块
List<XSLFShape> list = slide.getShapes();
for(XSLFShape xslfShape : list){
//判断是否为文本模块
if(xslfShape instanceof XSLFTextShape){
//获取文本框模块
XSLFTextShape textShape = (XSLFTextShape) xslfShape;
//获取文本框内的段落
for(XSLFTextParagraph paragraph : textShape.getTextParagraphs()){
//获取文本框段落的行文字
for(XSLFTextRun run : paragraph.getTextRuns()){
String text = run.getRawText();//行文字
run.setFontFamily("微软雅黑");//设置字体
run.setFontSize(32D);//设置大小
run.setBold(true);//是否加粗
//根据模板占位符 替换成 需要的值
text = text.replace(PlaceholderUtils.NAME, name);
run.setText(text);//数据写回文本框
}
}
}
}
}
//第四页 table表格数据处理 和Excel差不多
public static void four(XSLFSlide slide){
//获取所有模块
List<XSLFShape> list = slide.getShapes();
for(XSLFShape xslfShape : list){
//判断是否为表格模块
if(xslfShape instanceof XSLFTable){
//表格模块
XSLFTable tableRows = (XSLFTable) xslfShape;
//表格行
for(XSLFTableRow row : tableRows.getRows()){
//表格列
List<XSLFTableCell> cellList = row.getCells();
//这里是直接获取了第三列的数据
String text = row.getCells().get(3).getText();
//插入数据
row.getCells().get(3).setText(text);
//设置单列的数据样式
XSLFTextRun run = row.getCells().get(3).getTextParagraphs().get(0).getTextRuns().get(0);
run.setFontSize(14D);//数据大小
run.setFontFamily("微软雅黑");//数据字体
}
}
}
}