Java實現(xiàn)根據(jù)模板自動生成新的PPT
項目需求
最近項目中有一個需求就是讓Java代碼去代替人工操作,自動生成PPT,具體就是查詢數(shù)據(jù)庫數(shù)據(jù),然后根據(jù)模板文件(PPT),將數(shù)據(jù)庫數(shù)據(jù)與模板文件(PPT),進(jìn)行組合一下。生成新的PPT文件。
模板文件如下
將模板文件中的姓名,進(jìn)步率,連續(xù)進(jìn)步次數(shù),圖片。替換為具體的人員信息。
實現(xiàn)過程
1.引入第三方依賴
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency>
Apache POI 是用Java編寫的免費開源的跨平臺的 Java API,Apache POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫,意為“簡潔版的模糊實現(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>是我們項目自己定義的model,可改成其他業(yè)務(wù)的model if(powerPoint == null) { return; } //創(chuàng)建一個幻燈片 XMLSlideShow slideShow = new XMLSlideShow(powerPoint); //從幻燈片中獲取每個頁 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>是我們項目自己定義的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實現(xiàn)根據(jù)模板自動生成新的PPT的詳細(xì)內(nèi)容,更多關(guān)于Java的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java實現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02Java hashCode原理以及與equals()區(qū)別聯(lián)系詳解
在 Java 應(yīng)用程序執(zhí)行期間,在同一對象上多次調(diào)用 hashCode 方法時,必須一致地返回相同的整數(shù),前提是對象上 equals 比較中所用的信息沒有被修改。從某一應(yīng)用程序的一次執(zhí)行到同一應(yīng)用程序的另一次執(zhí)行,該整數(shù)無需保持一致2022-11-11