搜索
热搜: 活动 交友 discuz
查看: 11366|回复: 31

[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...
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-3 04:23:00 | 显示全部楼层

super online casino x78qcp


You definitely made the point!
online casino with signup bonus aussie casinos online gta online free car casino
gta online casino private dealer online casino new york rsweeps online casino at home
casino panama online slotocash casino no deposit bonus codes casino online bonos sin deposito
best online casino refer a friend bonus online casino michigan 2020 new online casinos
my live online casino best apps for sports betting resorts casino online coupon code
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-3 06:46:55 | 显示全部楼层

5 star online casino w56bvf

Edithpab ??? 2024-11-3 04:23
You definitely made the point!
online casino with signup bonus aussie casinos online gta online f ...


Cheers! Terrific information.
offerte casino online casino credit card casino free online stream
ndbc online casino online casino real money indiana online online casino
online casino max bet horse race betting 777 bet online casino login
gsn casino online indiana online casino best casino online payouts
casinos usa online poker sites real money legal online casinos in pa
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-3 12:04:43 | 显示全部楼层

jocuri casino gratis online a29vql

You stated it exceptionally well!
gta v online casino car this week best minnesota online casinos betsafe online casino
online casino with fish tables red dog casino 100 no deposit bonus codes online casinos with no deposit bonuses
the pokies online casino nba sports betting australian online casinos with no deposit bonus
online casino real money pennsylvania casino slots online arizona online casino
online casino real money in india no deposit online casino riversweeps online casino 777 download
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-3 17:03:14 | 显示全部楼层

free online casino no deposit bonus codes i84kue

Edithpab ??? 2024-11-3 04:23
You definitely made the point!
online casino with signup bonus aussie casinos online gta online f ...


You have made your point pretty clearly!.
online casinos with no deposit bonus codes safe casino games mafia 7777 casino online
casino online con bonus di benvenuto senza deposito cafe casino bonus codes las vegas casinos online slots
online casino self exclusion best apps for sports betting casino online 2021
online casino 100 bonus best online poker rooms igri online casino
online casino in Г¶sterreich blackjack online real money baccarat online usa casino bonus
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-3 17:28:15 | 显示全部楼层

best nj online casino bonus s68wvp

You actually revealed this effectively.
casino igre online free best online casino illinois real money free bonus casino games online
best online casino australia reviews mlb best bet online casinos tanzania
online casino payment system online craps casino online games that pay real money
us online casino sites red dog casino no deposit bonus codes online casino canada no deposit
casino online santa rosa la pampa ducky luck casino new online casino october 2023
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-3 17:33:40 | 显示全部楼层

online casino for blackjack b790cy

Edithpab ??? 2024-11-3 04:23
You definitely made the point!
online casino with signup bonus aussie casinos online gta online f ...


Superb information. Many thanks!
best promo codes for online casinos casino online games real money online casino legit sites
online casino courses real money online casino north carolina skycity online casino sign up
hungary online casino online betting are there any recordings of casino radio las vegas online
how to win from online casinos cafe casino no deposit wind creek online casino pa promo code
online casino bezahlen mit handy soccer betting sites in usa online casino roulette kostenlos
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-3 22:04:27 | 显示全部楼层

ruleta online casino gran madrid d96kiz

Edithpab ??? 2024-11-3 17:03
You have made your point pretty clearly!.
online casinos with no deposit bonus codes safe casin ...


Reliable knowledge. Kudos!
snelst uitbetalende online casino app for betting casino royale 2006 full movie online
best online casinos in australia casino in new zealand ainsworth online casinos
ocean monster online casino online bingo sites grand casino games online
online casinos reddit red dog casino 100 free chip best payout online casino uk
cleopatra casino game free online new pa online casino online casino no deposit no download
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-4 03:10:38 | 显示全部楼层

online free casino t86vjo


Nicely put, Thanks a lot.
yaamava online casino login san manuel mobile gambling app casino heist gta online payout
new online casinos for usa players bet soccer online stripper casino online
gta online kicked out of casino casino in auckland new zealand echeck deposit online casino
q casino online real money slot machines casino greek online
top 5 online casino real money crash gambling real money 21 casino online
回复

使用道具 举报

0

主题

2105

帖子

4308

积分

论坛元老

Rank: 8Rank: 8

积分
4308
发表于 2024-11-4 03:24:56 | 显示全部楼层

australian online casinos that accept paysafe v13dsb

Edithpab ??? 2024-11-4 03:10
Nicely put, Thanks a lot.
yaamava online casino login san manuel mobile gambling app casino heist ...


Thank you! I enjoy this.
online roulette casino australia las atlantis reviews casino online zonder registratie
riversweeps 777 online casino download real slot machines for real money schweizer online casino legal
online casino live tables online casinos north carolina draft kings online casino
gala casino online contact number tennessee online casinos best maine online casino sites
888 live casino online lucky tiger casino new pa casino online
回复

使用道具 举报

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

本版积分规则

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


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

GMT+8, 2024-11-23 23:12 , Processed in 0.082045 second(s), 22 queries .

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

© 2001-2013 Comsenz Inc.

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