利用Apache POI操作ppt模板
利用Apache POI操作ppt模板
实现的是读取ppt模板,修改动态数据,输出新ppt文件
构建的时maven项目所依赖如下
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 依赖引入--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.19</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>4.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.2.0</version></dependency><!-- 导出ppt的poi所需依赖--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.1</version><scope>compile</scope></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version><scope>compile</scope></dependency></dependencies>
阶段一
- 能修改模板中的数据,但是无法保留样式
- 注,模板中需要修改的数据 改为
{xxx}
的形式,不可重名,且目前测试不能用汉字。我多用数字代替
//这种方式改变模板数据,无法保留原有样式//读取原始模板ppt数据String modelPath = "D://统计结果导出.pptx";XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(modelPath));List<XSLFSlide> slides = slideShow.getSlides();for (XSLFSlide slide : slides) {List<XSLFShape> shapes = slide.getShapes();for (XSLFShape shape : shapes) {if (shape instanceof XSLFAutoShape) {XSLFAutoShape autoShape = (XSLFAutoShape) shape;String text = autoShape.getText();if (text.contains("{test}")) {XSLFTextRun textRun = autoShape.setText(text.replace("{test}", "改变成功"));textRun.setFontColor(Color.black);textRun.setFontFamily("微软雅黑");textRun.setFontSize(14.0);}}}}slideShow.write(new FileOutputStream("D://统计结果导出test.pptx"));
阶段二
- 利用paragraphs,可以保留原有样式
List<Stat> stats = this.statService.selectAll();stats.forEach(stat -> {String modelPath = "D://模板.pptx";XMLSlideShow slideShow = null;try {slideShow = new XMLSlideShow(new FileInputStream(modelPath));} catch (IOException e) {e.printStackTrace();}List<XSLFSlide> slides = slideShow.getSlides();for (XSLFSlide slide : slides){List<XSLFShape> shapes = slide.getShapes();for (XSLFShape shape : shapes){// 修改文本数据利用textshapeif (shape instanceof TextShape){List<XSLFTextParagraph> textParagraphs = ((TextShape) shape).getTextParagraphs();if (textParagraphs == null || textParagraphs.size() == 0){continue;}for (XSLFTextParagraph textParagraph : textParagraphs){if (textParagraph == null){continue;}List<XSLFTextRun> textRuns = textParagraph.getTextRuns();if (textRuns == null){continue;}for (XSLFTextRun textRun : textRuns){textRun.setText(textRun.getRawText().replace("{1}",stat.getRefPre()));}}}try {slideShow.write(new FileOutputStream("D://输出新文档test.pptx"));} catch (IOException e) {e.printStackTrace();}
- shape instanceof TextShape 只能操作文本数据
阶段三
修改ppt文档中的表格数据,利用 shape instanceof XSLFTable
List<Stat> stats = this.statService.selectAll();stats.forEach(stat -> {String modelPath = "D://模板.pptx";XMLSlideShow slideShow = null;try {slideShow = new XMLSlideShow(new FileInputStream(modelPath));} catch (IOException e) {e.printStackTrace();}List<XSLFSlide> slides = slideShow.getSlides();for (XSLFSlide slide : slides){List<XSLFShape> shapes = slide.getShapes();for (XSLFShape shape : shapes){// 修改表格数据 利用xslfTableelse if (shape instanceof XSLFTable){// XSLFTableCell cell = ((XSLFTable) shape).getCell(2, 2);int rows = ((XSLFTable) shape).getNumberOfRows();int columns = ((XSLFTable) shape).getNumberOfColumns();for (int i = 0; i< rows; i++){for (int j = 0;j < columns;j++){String text = ((XSLFTable) shape).getCell(i, j).getText();if (StringUtils.isNotBlank(text) && StringUtils.isNotEmpty(text)){// ((XSLFTable) shape).getCell(i,j).setText();((XSLFTable) shape).getCell(i,j).setText(((XSLFTable) shape).getCell(i,j).getText().replace("{call}",stat.getCall()));((XSLFTable) shape).getCell(i,j).setText(((XSLFTable) shape).getCell(i,j).getText().replace("{connect}",stat.getConnect()));((XSLFTable) shape).getCell(i,j).setText(((XSLFTable) shape).getCell(i,j).getText().replace("{noneCollect}",stat.getNoneCollect()));((XSLFTable) shape).getCell(i,j).setText(((XSLFTable) shape).getCell(i,j).getText().replace("{ref}",stat.getRef()));((XSLFTable) shape).getCell(i,j).setText(((XSLFTable) shape).getCell(i,j).getText().replace("{connectPre}",stat.getConnectPre()));((XSLFTable) shape).getCell(i,j).setText(((XSLFTable) shape).getCell(i,j).getText().replace("{refPre}",stat.getRefPre()));((XSLFTable) shape).getCell(i,j).setText(((XSLFTable) shape).getCell(i,j).getText().replace("{usefulCollect}",stat.getUsefulCollect()));}}}}}}try {slideShow.write(new FileOutputStream("D://输出新文件模板test.pptx"));} catch (IOException e) {e.printStackTrace();}
阶段四
自己手动创建一个带表格的ppt
String[] strings1 = new String[9];strings1= this.statService.selectAll();System.out.println(strings1[3]);System.out.println(strings1[5]);System.out.println("-=-=-=-=-=-=-=-=-=");String[] strings = new String[3];strings[0] = "hello";strings[1] = "hell";strings[2] = "hello13";*//** 加载PPT **//*XMLSlideShow ppt = new XMLSlideShow();*//** 创建一个slide,理解为PPT里的每一页 **//*XSLFSlide slide = ppt.createSlide();*//** 获得slideMasters**//*List<XSLFSlideMaster> slideMasters = ppt.getSlideMasters();*//** 创建表格**//*XSLFTable table = slide.createTable();*//** 设置表格 x ,y ,width,height **//*Rectangle2D rectangle2D = new Rectangle2D.Double(20, 90, 2200, 500);stats.forEach(stat -> {HashMap<String, Object> map = new HashMap<>();map.put("有效采集", stat.getUsefulCollect());map.put("接通", stat.getConnect());map.put("拒接", stat.getRef());map.put("未接通", stat.getNoneCollect());map.put("呼叫", stat.getCall());map.put("接通率", stat.getConnectPre());map.put("有效采集率接通", stat.getUsefulConnectCollect());map.put("有效采集率呼叫", stat.getUsefulCallCollect());map.put("拒接率", stat.getRefPre());System.out.println("=================");System.out.println(map);System.out.println("=================");Set<Map.Entry<String, Object>> set = map.entrySet();System.out.println(set);System.out.println("=================");Set<String> setkey = map.keySet();System.out.println(setkey);System.out.println("=================");//迭代输出map中的kvfor (String key : map.keySet()) {Object o = map.get(key);System.out.println(key + ":" + o);*//** 生成第一行 **//*XSLFTableRow firstRow = table.addRow();XSLFTableCell cell = firstRow.addCell();cell.setBorderColor(TableCell.BorderEdge.left, new Color(10, 100, 120));cell.setBorderColor(TableCell.BorderEdge.right, new Color(10, 100, 120));cell.setBorderColor(TableCell.BorderEdge.bottom, new Color(10, 100, 120));cell.setBorderColor(TableCell.BorderEdge.top, new Color(10, 100, 120));cell.setBorderWidth(TableCell.BorderEdge.left, 3);cell.setBorderWidth(TableCell.BorderEdge.right, 3);cell.setBorderWidth(TableCell.BorderEdge.top, 3);cell.setBorderWidth(TableCell.BorderEdge.bottom, 3);cell.setText(key + ":" + o);}});*//* *//** 生成第一个单元格**//*XSLFTableCell firstCell = firstRow.addCell();*//** 设置单元格的边框颜色 **//*firstCell.setBorderColor(TableCell.BorderEdge.left,new Color(10,100,120));firstCell.setBorderColor(TableCell.BorderEdge.right,new Color(10,100,120));firstCell.setBorderColor(TableCell.BorderEdge.top,new Color(10,100,120));firstCell.setBorderColor(TableCell.BorderEdge.bottom,new Color(10,100,120));*//** 设置单元格边框 **//*firstCell.setBorderWidth(TableCell.BorderEdge.left,3);firstCell.setBorderWidth(TableCell.BorderEdge.right,3);firstCell.setBorderWidth(TableCell.BorderEdge.top,3);firstCell.setBorderWidth(TableCell.BorderEdge.bottom,3);*//** 设置文本 **//*users.forEach(user -> {String name = user.getName();System.out.println("=============");firstCell.setText(name);});*//** 设置单元格的边框宽度 **//*
// 创建第一行第二个单元格XSLFTableCell secondCell = firstRow.addCell();secondCell.setText("sfdsf");*//** 设置单元格的边框颜色 **//*secondCell.setBorderColor(TableCell.BorderEdge.bottom,new Color(10,100,120));secondCell.setBorderColor(TableCell.BorderEdge.right,new Color(10,100,120));secondCell.setBorderColor(TableCell.BorderEdge.left,new Color(10,100,120));secondCell.setBorderColor(TableCell.BorderEdge.top,new Color(10,100,120));*//** 设置单元格边框 **//*secondCell.setBorderWidth(TableCell.BorderEdge.left,3);secondCell.setBorderWidth(TableCell.BorderEdge.right,3);secondCell.setBorderWidth(TableCell.BorderEdge.top,3);secondCell.setBorderWidth(TableCell.BorderEdge.bottom,3);table.setAnchor(rectangle2D);response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("统计结果导出.pptx", "UTF-8"));ServletOutputStream outputStream = response.getOutputStream();/** 文件路径 **/String filePath = "D://统计结果导出.pptx";/** 输出文件 **/ppt.write(outputStream);outputStream.close();ppt.close();ppt.write(new FileOutputStream(filePath));
利用Apache POI操作ppt模板相关推荐
- java word apache poi 操作word模板。
apache poi 操作word模板. 操作方式: 1.对于固定格,可以遍历格子然后替换其中指定的值例如在要替换的cell写入${example} 这样格式,遍历到之后替换. 2.对于需要增长的表格 ...
- java后台处理excel_java后台利用Apache poi 生成excel文档提供前台下载示例
之前在项目中会用到在java在后台把数据填入Word文档的模板来提供前台下载,为了自己能随时查看当时的实现方案及方便他人学习我写了这篇博客,访问量已经是我写的博客里第一了.于是乎我在学会用Java在后 ...
- java poi 操作ppt
java poi 操作ppt 可以参考: https://www.w3cschool.cn/apache_poi_ppt/apache_poi_ppt_installation.html http:/ ...
- java使用poi操作ppt(导入,导出,读取,添加,拼接,替换文本,页面排序)
POI操作PPT文档(导入,导出,读取,添加,拼接,替换文本,页面排序) 注意:直接读取.pptx文件时不能使用${user}这样的作为替换标识,在读取.pptx文件时会解析成三段文本"${ ...
- POI操作ppt图表完整示例演示
poi操作ppt图表史上最完整示例演示和内嵌excel的获取添加数据简单示例,POI3.15版本. 在模板中构造几中基本图表进行测试就行了. 其它操作ppt的基础资料见:http://blog.csd ...
- poi操作ppt图表史上最完整示例演示
poi操作ppt图表史上最完整示例演示和内嵌excel的获取添加数据简单示例 ,POI3.15版本. 在模板中构造几中基本图表进行测试就行了. 完整下载地址:http://download.csdn. ...
- poi 操作 PPT,针对 PPTX--文字操作
poi 操作 PPT,针对 PPTX–文字操作 文章目录 poi 操作 PPT,针对 PPTX--文字操作 1.获取幻灯片内的所有段落文字 2.对幻灯片内的文本内容进行模板替换 3.对段落新增文本 接 ...
- poi操作PPT文档总结
POI操作PPT文档1 注意ppt模板不能使用${user}--这样的作为替换标识,在读取PPT模板时会解析成三段文本1.${ 2.user 3.},而一般在控件中使用%1$S这样的方式做为替换标识 ...
- Apache POI操作Excel导出JAVABEAN对象方法
2019独角兽企业重金招聘Python工程师标准>>> Apache POI操作Excel导出方法说明 Apache的POI组件是Java操作Microsoft Office办公套件 ...
- poi 操作 PPT,针对 PPTX--图表篇
poi 操作 PPT,针对 PPTX–图表篇 文章目录 poi 操作 PPT,针对 PPTX--图表篇 1.读取 PPT 模板 2.替换标题 4.替换图表数据 接下来对 ppt 内的图表进行操作,替换 ...
最新文章
- Oracle数据库用户角色、表空间创建、删除命令
- pycharm运行出现ImportError:No module named
- python 删除链表中的重复元素
- 牛客 - sequence(笛卡尔树+线段树)
- ·MySQL数据库管理(SQL操作命令,解决忘记密码,设置用户权限)
- java 调用tomcat api,Tomcat采用双向认证https协议通过JavaAPI调用(一)配置SSL
- linux使用openssl查看文件的md5数值
- AVS 帧内预测模式的汇编优化
- 【C语言】递归详解汉诺塔问题
- windows/linux 下iozone参数详解
- 一个独到程序员的深刻见解(转)
- BufferedImage 图片背景色黑色问题
- crontab、cron、at、atq、batch、ps命令练习题
- Sage x3周期性凭证帮助企业提升财务效率
- 解读机械图样——剖视图
- Deepin安装向日葵报错
- #萌新日志#3.使用pix2pix CycleGAN和3d CycleGAN实现T1和T2加权模态的互转
- Altium Designer快捷键
- MOOC武汉大学SAS第2章作业
- 按摩肺经,补足肺气眠自安
热门文章
- 数学智力题 武士数独题目_数学智力题九宫格
- ExtJS 教程目录
- 金蝶KIS专业版二次开发探索
- 服务器系统上1068错误,一个backup exec 2012的真实故障案例,服务无法启动1068
- 屌丝最爱 蹭网卡 神器+使用说明
- 诺基亚n1系统更新显示无网络_塞班系统曾经的希望,诺基亚 N8-00 发布十周年回顾...
- 【基于JXTA的P2P应用开发】
- Abp Quartz配置Sqlite
- iOS视频裁剪、拼接、添加水印、添加背景音乐
- 19.深入浅出:正弦波振荡电路——参考《模拟电子技术基础》清华大学华成英主讲