最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)根據(jù)模板自動(dòng)生成新的PPT

 更新時(shí)間:2022年02月22日 15:05:36   作者:小小張自由—>張有博  
這篇文章主要介紹了如何利用Java代碼自動(dòng)生成PPT,具體就是查詢數(shù)據(jù)庫數(shù)據(jù),然后根據(jù)模板文件(PPT),將數(shù)據(jù)庫數(shù)據(jù)與模板文件(PPT),進(jìn)行組合一下,生成新的PPT文件。感興趣的可以了解一下

項(xiàng)目需求

最近項(xiàng)目中有一個(gè)需求就是讓Java代碼去代替人工操作,自動(dòng)生成PPT,具體就是查詢數(shù)據(jù)庫數(shù)據(jù),然后根據(jù)模板文件(PPT),將數(shù)據(jù)庫數(shù)據(jù)與模板文件(PPT),進(jìn)行組合一下。生成新的PPT文件。

模板文件如下

將模板文件中的姓名,進(jìn)步率,連續(xù)進(jìn)步次數(shù),圖片。替換為具體的人員信息。

實(shí)現(xiàn)過程

1.引入第三方依賴

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>3.15</version>
</dependency>

Apache POI   是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,Apache POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫,意為“簡潔版的模糊實(shí)現(xiàn)”。

HSSF-提供讀寫MicrosoftExcelXLS格式檔案的功能

XSSF-提供讀寫MicrosoftExcelOOXMLXLSX格式檔案的功能

HWPF-提供讀寫MicrosoftWordDOC格式檔案的功能

HSLF-提供讀寫MicrosoftPowerPoint格式檔案的功能

HDGF-提供讀MicrosoftVisio格式檔案的功能

HPBF-提供讀MicrosoftPublisher格式檔案的功能

HSMF-提供讀MicrosoftOutlook格式檔案的功能

2.編寫業(yè)務(wù)代碼

 
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
 
import java.io.*;
import java.util.List;
 
/**
 * 讀取模板PPT生成新的PPT文件
 *
 * @author Promsing(張有博)
 * @version 1.0.0
 * @since 2022/2/11 - 15:37
 */
public class RenderPowerPointTemplate extends BasePowerPointFileUtil {
 
    /**
     * 讀取PPT模板
     * @param powerPoint
     * @param 
     * @throws IOException
     */
    public static void renderPowerPointTemplateOfCertificate(InputStream powerPoint, List<WeekAnalyseModel> lists, String rankType) throws IOException {
        //List<WeekAnalyseModel>是我們項(xiàng)目自己定義的model,可改成其他業(yè)務(wù)的model
        if(powerPoint == null) {
            return;
        }
        //創(chuàng)建一個(gè)幻燈片
        XMLSlideShow slideShow = new XMLSlideShow(powerPoint);
        //從幻燈片中獲取每個(gè)頁
        List slides = slideShow.getSlides();
        //遍歷每一頁P(yáng)PT
        for (int i = 0 ; i < slides.size() ; i++) {
            //幻燈片布局,文本框,矩形框之類的,遍歷一頁P(yáng)PT中的所有控件
            List shapes = ((Slide)slides.get(i)).getShapes();
            for (int j = 0 ; j < shapes.size() ; j++) {
                Shape shape = (Shape) shapes.get(j);
                RenderPowerPointTemplate.renderShapeAndPicture(shape, lists.get(i),rankType);
            }
        }
 
        //新PPT的位置,file就是新的PPT文件
        File file=new File(rankType+"test.pptx");
        OutputStream outputStreams = new FileOutputStream(file);
        slideShow.write(outputStreams);
       // FileUpLoadUtil.T_THREAD_LOCAL.set(file.getAbsolutePath());
        System.out.println("新文件的路徑:"+file.getAbsolutePath());
 
 
    }
}
 
import com.tfjybj.integral.constant.CommonConstant;
import com.tfjybj.integral.model.WeekAnalyseModel;
import com.tfjybj.integral.utils.SimplifiedDate;
import org.apache.commons.io.FileUtils;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.*;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
 
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
/**
 * <p>PowerPoint文件工具基類
 * <p>
 * <p>通用的PowerPoint文件工具基類,可用于從PowerPoint文檔中抽取文本信息
 */
public class BasePowerPointFileUtil {
 
 
    
    /**
     * 渲染、繪制文本框
     *
     * @param shape
     * @param data
     */
    public static void renderShapeAndPicture(Shape shape, WeekAnalyseModel data,String rankType) {
        //判斷是否是文本框
        if (shape instanceof TextShape) {
            BasePowerPointFileUtil.replace(shape, data,rankType);
        } else if (shape instanceof GroupShape) {
            Iterator groupShapes = ((GroupShape) shape).iterator();
            while (groupShapes.hasNext()) {
                Shape groupShape = (Shape) groupShapes.next();
                BasePowerPointFileUtil.renderShapeAndPicture(groupShape, data,rankType);
            }
        } else if (shape instanceof TableShape) {
            TableShape tableShape = ((TableShape) shape);
            int column = tableShape.getNumberOfColumns();
            int row = tableShape.getNumberOfRows();
            for (int r = 0; r < row; r++) {
                for (int c = 0; c < column; c++) {
                    BasePowerPointFileUtil.replace(tableShape.getCell(r, c), data,rankType);
                }
            }
        } else if (shape instanceof PictureShape) {
            //判斷是否是圖片框
            PictureShape pictureShape = (PictureShape) shape;
            PictureData pictureData = pictureShape.getPictureData();
            byte[] bytes = BufferStreamForByte(URLToFile(data.getPictureURL()), 1024);
            try {
                pictureData.setData(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
    }
 
 
    /**
     * 替換模板PPT中的值
     *
     * @param shape
     * @param weekAnalyseModel
     */
    public static void replace(Shape shape, WeekAnalyseModel weekAnalyseModel,String rankType) {
         //List<WeekAnalyseModel>是我們項(xiàng)目自己定義的model,可改成其他業(yè)務(wù)的model
        if (shape instanceof TextShape) {
 
            String replaceText = ((XSLFTextShape) shape).getText();
            XSLFTextRun xslfTextRun = null;
            //替換數(shù)據(jù)的業(yè)務(wù)邏輯,待優(yōu)化
            switch (replaceText) {
                case "姓名:閃耀姓名1":
                    xslfTextRun = ((XSLFTextShape) shape).setText("姓名:" + weekAnalyseModel.getUserName());
                    break;
                case "積分:閃耀分?jǐn)?shù)1":
                    xslfTextRun = ((XSLFTextShape) shape).setText("積分:" + weekAnalyseModel.getWeekData());
                    break;
                case "閃耀1連擊ヾ":
                    xslfTextRun = ((XSLFTextShape) shape).setText("閃耀" + weekAnalyseModel.getListNumber() + "連擊ヾ");
                    break;
                case "姓名:閃耀姓名2":
                    xslfTextRun = ((XSLFTextShape) shape).setText("姓名:" + weekAnalyseModel.getUserName());
                    break;
                case "積分:閃耀分?jǐn)?shù)2":
                    xslfTextRun = ((XSLFTextShape) shape).setText("積分:" + weekAnalyseModel.getWeekData());
                    break;
                case "閃耀2連擊ヾ":
                    xslfTextRun = ((XSLFTextShape) shape).setText("閃耀" + weekAnalyseModel.getListNumber() + "連擊ヾ");
                    break;
               
            }
 
            //空值過濾,設(shè)置樣式
            if (xslfTextRun != null) {
                if (rankType.equals("閃耀之星")||rankType.equals("進(jìn)步之星")){
                    setTextStyle(xslfTextRun);
                }else if (rankType.equals("閃耀之星榮譽(yù)證書")||rankType.equals("進(jìn)步之星榮譽(yù)證書")){
                    setTextStyleCertificate(xslfTextRun);
                }
            }
        }
    }
 
    /**
     * 設(shè)置字體樣式
     *
     * @param xslfTextRun
     */
    private static void setTextStyle(XSLFTextRun xslfTextRun) {
        xslfTextRun.setFontFamily("等線(正文)");
        Color color = new Color(255, 255, 255);
        xslfTextRun.setFontColor(color);
        xslfTextRun.setFontSize(40.0);
        xslfTextRun.setBold(true);
    }
 
    /**
     * 設(shè)置證書字體樣式
     *
     * @param xslfTextRun
     */
    private static void setTextStyleCertificate(XSLFTextRun xslfTextRun) {
        xslfTextRun.setFontFamily("宋體");
        Color color = new Color(0, 0, 0);
        xslfTextRun.setFontColor(color);
        xslfTextRun.setFontSize(32.0);
        xslfTextRun.setBold(true);
    }
 
    /**
     * 將文件轉(zhuǎn)為字節(jié)數(shù)組
     * @param file
     * @param size
     * @return
     */
    public static byte[] BufferStreamForByte(File file, int size) {
        byte[] content = null;
        try {
            BufferedInputStream bis = null;
            ByteArrayOutputStream out = null;
            try {
                FileInputStream input = new FileInputStream(file);
                bis = new BufferedInputStream(input, size);
                byte[] bytes = new byte[1024];
                int len;
                out = new ByteArrayOutputStream();
                while ((len = bis.read(bytes)) > 0) {
                    out.write(bytes, 0, len);
                }
 
                bis.close();
                content = out.toByteArray();
            } finally {
                if (bis != null) {
                    bis.close();
                }
                if (out != null) {
                    out.close();
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return content;
 
    }
 
    /**
     * 讀取網(wǎng)絡(luò)中的圖片
     * @param url https://www.kziyue.com/wp-content/uploads/2019/06/5bca-hxyuaph9825616.jpg
     * @return
     */
    public static File URLToFile(String url){
        File file1 = new File("test.mp4");
        try {
 
            URL url1 = new URL(url);
            FileUtils.copyURLToFile(url1,file1);
 
        } catch (IOException e) {
            e.printStackTrace();
        }
        File absoluteFile = file1.getAbsoluteFile();
        return file1;
    }
 
 
}

3.根據(jù)模板生成新的PPT

以上就是Java實(shí)現(xiàn)根據(jù)模板自動(dòng)生成新的PPT的詳細(xì)內(nèi)容,更多關(guān)于Java的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot基于Redis實(shí)現(xiàn)token的在線續(xù)期的實(shí)踐

    SpringBoot基于Redis實(shí)現(xiàn)token的在線續(xù)期的實(shí)踐

    本文主要介紹了使用Redis實(shí)現(xiàn)JWT令牌在線續(xù)期的方案,通過在線續(xù)期token,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • Java中報(bào)錯(cuò)org.springframework.jdbc.UncategorizedSQLException的多種解決方法

    Java中報(bào)錯(cuò)org.springframework.jdbc.UncategorizedSQLException的多種

    本文主要介紹了Java中報(bào)錯(cuò)org.springframework.jdbc.UncategorizedSQLException的多種解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java實(shí)現(xiàn)UDP互發(fā)消息

    Java實(shí)現(xiàn)UDP互發(fā)消息

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)UDP互發(fā)消息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • 如何利用Stream改變list中特定對象的某一屬性

    如何利用Stream改變list中特定對象的某一屬性

    這篇文章主要介紹了如何利用Stream改變list中特定對象的某一屬性問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java數(shù)據(jù)溢出代碼詳解

    Java數(shù)據(jù)溢出代碼詳解

    這篇文章主要介紹了Java數(shù)據(jù)溢出的相關(guān)內(nèi)容,包括具體代碼示例,分析比較詳細(xì),希望對大家有所幫助,感興趣的朋友可以參考下。
    2017-09-09
  • Java實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出的步驟詳解

    Java實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出的步驟詳解

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出,文中示例代碼介紹的非常詳細(xì),對我們的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴們可以參考一下
    2023-06-06
  • 詳解Java線性結(jié)構(gòu)中的鏈表

    詳解Java線性結(jié)構(gòu)中的鏈表

    除了一些算法之外,我們還有掌握一些常見的數(shù)據(jù)結(jié)構(gòu),比如數(shù)組、鏈表、棧、隊(duì)列、樹等結(jié)構(gòu),所以接下來就給大家詳細(xì)講解一下線性結(jié)構(gòu)中的鏈表,需要的朋友可以參考下
    2023-07-07
  • Java編程使用UDP建立群聊系統(tǒng)代碼實(shí)例

    Java編程使用UDP建立群聊系統(tǒng)代碼實(shí)例

    這篇文章主要介紹了Java編程使用UDP建立群聊系統(tǒng)代碼實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2018-01-01
  • Java后端限制頻繁請求和重復(fù)提交的實(shí)現(xiàn)

    Java后端限制頻繁請求和重復(fù)提交的實(shí)現(xiàn)

    很多用戶會(huì)請求過于頻繁或者是多次重復(fù)提交數(shù)據(jù),本文主要介紹了Java后端限制頻繁請求和重復(fù)提交的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • SpringBoot實(shí)現(xiàn)yml配置文件為變量賦值

    SpringBoot實(shí)現(xiàn)yml配置文件為變量賦值

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)yml配置文件為變量賦值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評論

秭归县| 石门县| 延吉市| 阿拉善盟| 广德县| 文化| 腾冲县| 滨海县| 左贡县| 共和县| 方城县| 怀集县| 泰安市| 壤塘县| 宁强县| 玛纳斯县| 沾化县| 突泉县| 常州市| 潜山县| 汝阳县| 平凉市| 武平县| 北票市| 平陆县| 恭城| 任丘市| 托里县| 阳高县| 兴隆县| 孟村| 宝山区| 高邮市| 故城县| 云南省| 万山特区| 景泰县| 察隅县| 星子县| 乐都县| 长垣县|