Java導出PPT過程


業務場景根據PPT模板填充數據后導出。

1.前台通過axiso發送導出請求。

2.導入java-poi相關依賴的jar包

3.讀取resource下的ppt模板

4.將讀取的ppt模板文件輸入流,通過POI轉換為ppt對象,並且動態創建幻燈片。

5.根據ppt模板填充業務數據。

6.導出ppt文件(注意響應類型)。

7.前台利用blob對象對響應轉為具體文件。(參考鏈接:https://my.oschina.net/u/4348174/blog/3564571

// 導出ppt,axiso前台
exportPPT(){
    axios({
        url: '',
        method: 'post',
        data: {},
        responseType: 'blob'
      }).then(response => {
        const blob = new Blob([response.data])
        const fileName = this.fileName+ '.ppt'
        if ('download' in document.createElement('a')) { // 非IE下載
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 釋放URL 對象
          document.body.removeChild(elink)
          this.disable = false
        } else { // IE10+下載
          navigator.msSaveBlob(blob, fileName)
          this.disable = false
        }
      })  
}
// 獲取配置文件的方法
// 方法一:示例獲取src目錄下config目錄中application.properties配置文件
ResourceBundle resource = ResourceBundle.getBundle("config/application");
String key = resource.getString("port"); 

// 方法二:示例獲取src獲取resource下application.properties
InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("application.properties");  
Properties prop = new Properties();  
prop.load(inStream);  
String key = prop.getProperty("port");  
// 這種方式,也可以讀取其他類型的文件,eg:ppt文件
InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("PPT文件");  
// java,poi相關對象和接口
XMLSlideShow ppt = new XMLSlideShow(); // PPT
XSLFSlide slide = ppt.createSlide(); // 幻燈片


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM