搜索
热搜: 活动 交友 discuz
查看: 10391|回复: 0

[JAVA] PPT导出-模板和自定义处理方案

[复制链接]

57

主题

58

帖子

1232

积分

超级版主

Rank: 8Rank: 8

积分
1232
发表于 2024-6-21 17:36:51 | 显示全部楼层 |阅读模式
本帖最后由 Maybe 于 2024-6-21 17:38 编辑

​ 两种方式:1.自定义样式导出  2.根据模板导出
1.自定义设置样式导出PPT

  1.    public static void main(String[] args) throws IOException {
  2.         XMLSlideShow ppt = new XMLSlideShow();
  3.         // 创建幻灯片
  4.         XSLFSlide slide = ppt.createSlide();

  5.         // 创建文本框
  6.         XSLFTextBox textBox = slide.createTextBox();
  7.         // x y设置距离  w h 设置大小
  8.         textBox.setAnchor(new Rectangle2D.Double(30,0, 600, 50));
  9.         // 设置文本框的内容
  10.         textBox.addNewTextParagraph().addNewTextRun().setText("姓名");


  11.         // 创建文本框
  12.         XSLFTextBox textBox1 = slide.createTextBox();
  13.         // x y设置距离  w h 设置大小
  14.         textBox1.setAnchor(new Rectangle2D.Double(30,50, 600, 50));
  15.         // 设置文本框的内容
  16.         textBox1.addNewTextParagraph().addNewTextRun().setText("年龄");
  17.         // 创建文本框
  18.         XSLFTextBox textBox11 = slide.createTextBox();
  19.         // x y设置距离  w h 设置大小
  20.         textBox11.setAnchor(new Rectangle2D.Double(250,50, 600, 50));
  21.         // 设置文本框的内容
  22.         textBox11.addNewTextParagraph().addNewTextRun().setText("性别");

  23.         // 创建文本框
  24.         XSLFTextBox textBox2 = slide.createTextBox();
  25.         // x y设置距离  w h 设置大小
  26.         textBox2.setAnchor(new Rectangle2D.Double(30,100, 600, 50));
  27.         // 设置文本框的内容
  28.         textBox2.addNewTextParagraph().addNewTextRun().setText("时间");
  29.         // 创建文本框
  30.         XSLFTextBox textBox22 = slide.createTextBox();
  31.         // x y设置距离  w h 设置大小
  32.         textBox22.setAnchor(new Rectangle2D.Double(250,100, 600, 50));
  33.         // 设置文本框的内容
  34.         textBox22.addNewTextParagraph().addNewTextRun().setText("地点");

  35.         // 创建文本框
  36.         XSLFTextBox textBox3 = slide.createTextBox();
  37.         // x y设置距离  w h 设置大小
  38.         textBox3.setAnchor(new Rectangle2D.Double(30,150, 600, 50));
  39.         // 设置文本框的内容
  40.         textBox3.addNewTextParagraph().addNewTextRun().setText("事件"
  41.         // 创建文本框
  42.         XSLFTextBox textBox33 = slide.createTextBox();
  43.         // x y设置距离  w h 设置大小
  44.         textBox33.setAnchor(new Rectangle2D.Double(250,150, 600, 50));
  45.         // 设置文本框的内容
  46.         textBox33.addNewTextParagraph().addNewTextRun().setText("备注");


  47.         // 插入图片
  48.         // 获取图片的file对象
  49.         File file = new File("C:\\图片.png");
  50.         // 获取字节流
  51.         byte[] bt = FileUtils.readFileToByteArray(file);
  52.         XSLFPictureData idx = ppt.addPicture(bt, PictureData.PictureType.PNG);
  53.         // 插入图片
  54.         XSLFPictureShape pic = slide.createPicture(idx);
  55.         pic.setAnchor(new Rectangle2D.Double(30,200,350,150));


  56.         // 写入ppt中
  57.         ppt.write(new FileOutputStream("输出PPT"+".pptx"));
  58.     }
复制代码


2.模板处理
  1. import com.oppo.ipd.plm.mechanical.web.structure.PPTExport.bean.WeekAnalyseModel;
  2. import org.apache.commons.io.FileUtils;
  3. import org.apache.poi.sl.usermodel.Shape;
  4. import org.apache.poi.sl.usermodel.*;
  5. import org.apache.poi.xslf.usermodel.XSLFTextRun;
  6. import org.apache.poi.xslf.usermodel.XSLFTextShape;

  7. import java.awt.*;
  8. import java.io.*;
  9. import java.net.URL;
  10. import java.util.Iterator;

  11. /**
  12. * <p>PowerPoint文件工具基类
  13. * <p>
  14. * <p>通用的PowerPoint文件工具基类,可用于从PowerPoint文档中抽取文本信息
  15. */
  16. public class BasePowerPointFileUtil {



  17.     /**
  18.      * 渲染、绘制文本框
  19.      *
  20.      * @param shape
  21.      * @param data
  22.      */
  23.     public static void renderShapeAndPicture(Shape shape, WeekAnalyseModel data, String rankType) {
  24.         //判断是否是文本框
  25.         if (shape instanceof TextShape) {
  26.             BasePowerPointFileUtil.replace(shape, data,rankType);
  27.         } else if (shape instanceof GroupShape) {
  28.             Iterator groupShapes = ((GroupShape) shape).iterator();
  29.             while (groupShapes.hasNext()) {
  30.                 Shape groupShape = (Shape) groupShapes.next();
  31.                 BasePowerPointFileUtil.renderShapeAndPicture(groupShape, data,rankType);
  32.             }
  33.         } else if (shape instanceof TableShape) {
  34.             TableShape tableShape = ((TableShape) shape);
  35.             int column = tableShape.getNumberOfColumns();
  36.             int row = tableShape.getNumberOfRows();
  37.             for (int r = 0; r < row; r++) {
  38.                 for (int c = 0; c < column; c++) {
  39.                     BasePowerPointFileUtil.replace(tableShape.getCell(r, c), data,rankType);
  40.                 }
  41.             }
  42.         } else if (shape instanceof PictureShape) {
  43.             //判断是否是图片框
  44.             PictureShape pictureShape = (PictureShape) shape;
  45.             PictureData pictureData = pictureShape.getPictureData();
  46.             byte[] bytes = BufferStreamForByte(URLToFile(data.getPictureURL()), 1024);
  47.             try {
  48.                 pictureData.setData(bytes);
  49.             } catch (IOException e) {
  50.                 e.printStackTrace();
  51.             }
  52.         }

  53.     }


  54.     /**
  55.      * 替换模板PPT中的值
  56.      *
  57.      * @param shape
  58.      * @param weekAnalyseModel
  59.      */
  60.     public static void replace(Shape shape, WeekAnalyseModel weekAnalyseModel,String rankType) {
  61.         //List<WeekAnalyseModel>是我们项目自己定义的model,可改成其他业务的model
  62.         if (shape instanceof TextShape) {

  63.             String replaceText = ((XSLFTextShape) shape).getText();
  64.             XSLFTextRun xslfTextRun = null;
  65.             //替换数据的业务逻辑,待优化
  66.             switch (replaceText) {
  67.                 case "{name}":
  68.                     xslfTextRun = ((XSLFTextShape) shape).setText(weekAnalyseModel.getName());
  69.                     break;
  70.                 case "{person}":
  71.                     xslfTextRun = ((XSLFTextShape) shape).setText(weekAnalyseModel.getPerson());
  72.                     break;

  73.             }

  74.             //空值过滤,设置样式
  75.             if (xslfTextRun != null) {
  76.                 if (rankType.equals("未定义")||rankType.equals("未定义")){
  77.                     setTextStyle(xslfTextRun);
  78.                 }else if (rankType.equals("未定义")||rankType.equals("未定义")){
  79.                     setTextStyleCertificate(xslfTextRun);
  80.                 }
  81.             }
  82.         }
  83.     }

  84.     /**
  85.      * 设置字体样式
  86.      *
  87.      * @param xslfTextRun
  88.      */
  89.     private static void setTextStyle(XSLFTextRun xslfTextRun) {
  90.         xslfTextRun.setFontFamily("等线(正文)");
  91.         Color color = new Color(255, 255, 255);
  92.         xslfTextRun.setFontColor(color);
  93.         xslfTextRun.setFontSize(40.0);
  94.         xslfTextRun.setBold(true);
  95.     }

  96.     /**
  97.      * 设置证书字体样式
  98.      *
  99.      * @param xslfTextRun
  100.      */
  101.     private static void setTextStyleCertificate(XSLFTextRun xslfTextRun) {
  102.         xslfTextRun.setFontFamily("宋体");
  103.         Color color = new Color(0, 0, 0);
  104.         xslfTextRun.setFontColor(color);
  105.         xslfTextRun.setFontSize(32.0);
  106.         xslfTextRun.setBold(true);
  107.     }

  108.     /**
  109.      * 将文件转为字节数组
  110.      * @param file
  111.      * @param size
  112.      * @return
  113.      */
  114.     public static byte[] BufferStreamForByte(File file, int size) {
  115.         byte[] content = null;
  116.         try {
  117.             BufferedInputStream bis = null;
  118.             ByteArrayOutputStream out = null;
  119.             try {
  120.                 FileInputStream input = new FileInputStream(file);
  121.                 bis = new BufferedInputStream(input, size);
  122.                 byte[] bytes = new byte[1024];
  123.                 int len;
  124.                 out = new ByteArrayOutputStream();
  125.                 while ((len = bis.read(bytes)) > 0) {
  126.                     out.write(bytes, 0, len);
  127.                 }

  128.                 bis.close();
  129.                 content = out.toByteArray();
  130.             } finally {
  131.                 if (bis != null) {
  132.                     bis.close();
  133.                 }
  134.                 if (out != null) {
  135.                     out.close();
  136.                 }
  137.             }
  138.         } catch (IOException e) {
  139.             // TODO Auto-generated catch block
  140.             e.printStackTrace();
  141.         }
  142.         return content;

  143.     }

  144.     /**
  145.      * 读取网络中的图片
  146.      * @param url https://www.kziyue.com/wp-content/uploads/2019/06/5bca-hxyuaph9825616.jpg
  147.      * @return
  148.      */
  149.     public static File URLToFile(String url){
  150.         File file1 = new File("replace.png");
  151.         try {

  152.             URL url1 = new URL(url);
  153.             FileUtils.copyURLToFile(url1,file1);

  154.         } catch (IOException e) {
  155.             e.printStackTrace();
  156.         }
  157.         File absoluteFile = file1.getAbsoluteFile();
  158.         return file1;
  159.     }


  160. }
复制代码


3.内容实体类
  1. import lombok.Data;

  2. @Data
  3. public class WeekAnalyseModel {

  4.     private String name;
  5.     private String pictureURL;
  6.     private String person;
  7. }
复制代码


4.测试代码:
  1.    public static void main(String[] args) throws Exception {
  2.         //载入数据
  3.         WeekAnalyseModel weekAnalyseModel=new WeekAnalyseModel();
  4.         weekAnalyseModel.setName("数据来源1");
  5.         weekAnalyseModel.setPerson("数据来源1");
  6.         weekAnalyseModel.setPictureURL("http://dev.itqu.net/1.jpg");


  7.         WeekAnalyseModel weekAnalyseModel2=new WeekAnalyseModel();
  8.         weekAnalyseModel2.setName("数据来源2");
  9.         weekAnalyseModel2.setPerson("数据来源2");
  10.         weekAnalyseModel2.setPictureURL("http://dev.itqu.net/2.jpg");

  11.         WeekAnalyseModel weekAnalyseModel3=new WeekAnalyseModel();
  12.         weekAnalyseModel3.setName("数据来源3");
  13.         weekAnalyseModel3.setPerson("数据来源3");
  14.         weekAnalyseModel3.setPictureURL("http://dev.itqu.net/3.jpg");

  15.         ArrayList arrayList=new ArrayList();
  16.         arrayList.add(weekAnalyseModel);
  17.         arrayList.add(weekAnalyseModel2);
  18.         arrayList.add(weekAnalyseModel3);

  19.         //载入模板ppt,创建模板副本
  20.         InputStream inputStream = new FileInputStream("model.pptx");
  21.         copyFile("model.pptx","model-copy.pptx");
  22.         InputStream inputStreamCopy = new FileInputStream("model-copy.pptx");

  23.         RenderPowerPointTemplate.renderPowerPointTemplateOfCertificate(inputStreamCopy,arrayList,"out");


  24.     }

  25.     private static void copyFile(String source, String copy) {
  26.         Path sourcePath = Paths.get(source);
  27.         Path destinationPath = Paths.get(copy);
  28.         try {
  29.             Files.copy(sourcePath, destinationPath,StandardCopyOption.REPLACE_EXISTING);
  30.         } catch (FileAlreadyExistsException e) {
  31.             // 目标文件已经存在
  32.         } catch (IOException e) {
  33.             // 发生了其他错误
  34.             e.printStackTrace();
  35.         }
  36.     }
复制代码








上一篇:JAVA接入讯虎支付接口调用
下一篇:Host '***' is blocked because of many connection errors; unblock wi...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

抖音账号
关注抖音
加入粉丝群,抽取论坛金币


Archiver|手机版|小黑屋|IT趣-GEEK社区

GMT+8, 2024-10-18 13:30 , Processed in 0.273475 second(s), 22 queries .

Powered by Discuz! X3.4. 技术支持 by 巅峰设计

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表