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

java通過jacob實現(xiàn)office在線預(yù)覽功能

 更新時間:2019年08月21日 15:25:18   作者:一只仰望天空的菜鳥  
這篇文章主要為大家詳細介紹了java通過jacob實現(xiàn)office在線預(yù)覽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

簡介:

這篇文章中的代碼都是參考于網(wǎng)上的,只做一個記錄。主要做的就是實現(xiàn)一個office在線預(yù)覽功能。

第一步:裝office

第二步:下載jacob

打開網(wǎng)址下載,目前最新的是1.19版本。

第三步:配置jdk

解壓下載完的jacob壓縮包,根據(jù)jdk的版本選擇dll中的一個,放入/jdk/jre/bin中。

第四步:在項目中引入jar包

在maven官網(wǎng)上找不到com.jacob的jar包,只能手動引入,這個jar包在jacob的壓縮包中有。

<dependency>
 <groupId>com.jacob</groupId>
 <artifactId>jacob</artifactId>
 <version>1.19</version>
 <scope>system</scope>
 <systemPath>${project.basedir}/lib/jacob.jar</systemPath>
</dependency>

第五步:將office轉(zhuǎn)化為pdf文件

這里需要再次說明,這個代碼不是我寫的,這里只是做個記錄,方便下次用到的時候直接使用。

import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;

@RestController
public class PdfConvert {

 @RequestMapping("/PdfConvert.do")
 public void PdfConvert(HttpServletResponse response) {
 String path = "C:\\Users\\acer\\Desktop\\測試.doc";
 String path2 = "C:\\Users\\acer\\Desktop\\測試.pdf";
 word2PDF(path, path2);
 String path3 = "C:\\Users\\acer\\Desktop\\測試2.ppt";
 String path4 = "C:\\Users\\acer\\Desktop\\測試2.pdf";
 ppt2PDF(path3, path4);
 String path5 = "C:\\Users\\acer\\Desktop\\測試3.xls";
 String path6 = "C:\\Users\\acer\\Desktop\\測試3.pdf";
 excel2PDF(path5, path6);
 }

 public boolean word2PDF(String inputFile, String pdfFile) {
 ActiveXComponent app = new ActiveXComponent("Word.Application");
 try {
  app.setProperty("Visible", false);
  Dispatch docs = app.getProperty("Documents").toDispatch();
  Dispatch doc = Dispatch.call(docs, "Open", new Object[]{inputFile, false, true}).toDispatch();
  Dispatch.call(doc, "ExportAsFixedFormat", new Object[]{pdfFile, 17});
  Dispatch.call(doc, "Close", new Object[]{false});
  app.invoke("Quit", 0);
  return true;
 } catch (Exception var6) {
  app.invoke("Quit", 0);
  return false;
 }
 }

 public boolean excel2PDF(String inputFile, String pdfFile) {
 ComThread.InitSTA(true);
 ActiveXComponent app = new ActiveXComponent("Excel.Application");
 try {
  app.setProperty("Visible", false);
  app.setProperty("AutomationSecurity", new Variant(3));
  Dispatch excels = app.getProperty("Workbooks").toDispatch();
  Dispatch excel = Dispatch.invoke(excels, "Open", 1, new Object[]{inputFile, new Variant(false), new Variant(false)}, new int[9]).toDispatch();
  Dispatch.invoke(excel, "ExportAsFixedFormat", 1, new Object[]{new Variant(0), pdfFile, new Variant(0)}, new int[1]);
  Dispatch.call(excel, "Close", new Object[]{false});
  if (app != null) {
  app.invoke("Quit", new Variant[0]);
  app = null;
  }

  ComThread.Release();
  return true;
 } catch (Exception var6) {
  app.invoke("Quit");
  return false;
 }
 }

 public boolean ppt2PDF(String inputFile, String pdfFile) {
 ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");

 try {
  Dispatch ppts = app.getProperty("Presentations").toDispatch();
  Dispatch ppt = Dispatch.call(ppts, "Open", new Object[]{inputFile, true, true, false}).toDispatch();
  Dispatch.call(ppt, "SaveAs", new Object[]{pdfFile, 32});
  Dispatch.call(ppt, "Close");
  app.invoke("Quit");
  return true;
 } catch (Exception var6) {
  app.invoke("Quit");
  return false;
 }
 }
}

第六步:在頁面上展示pdf

后端:

@RequestMapping("/GetPdf.do")
 public void GetPdf(HttpServletResponse response) {
 //從數(shù)據(jù)庫中查出文件位置和文件名字
 String pdfpath = "C:\\Users\\acer\\Desktop\\測試.pdf";
 String pdfname = "測試";
 try {
  File file = new File(pdfpath);
  if (!file.exists()) {
  response.getWriter().write("該文檔生成pdf失敗,請下載文檔查看");
  return;
  }
  InputStream fis = new FileInputStream(pdfpath);
  byte[] buffer = new byte[1024];
  response.reset();
  response.addHeader("Content-Disposition", "inline;filename=" + java.net.URLEncoder.encode(pdfname, "UTF-8"));
  response.addHeader("Content-Length", "" + file.length());
  response.setContentType("application/pdf");
  OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
  int nbytes = 0;
  while ((nbytes = fis.read(buffer)) != -1) {
  toClient.write(buffer, 0, nbytes);
  toClient.flush();
  }
  toClient.flush();
  toClient.close();
  fis.close();
 } catch (Exception ex) {
  ex.printStackTrace();
 }
 }

前端:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8"/>
 <title>pdf在線預(yù)覽</title>
</head>
<body>
<div style=" width: 100%; height: 100%;">
 <embed src="/GetPdf.do"
  type="application/pdf" style="overflow: auto; width: 100%; height: 800px;" />
</div>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java實現(xiàn)RedisTemplate操作哈希數(shù)據(jù)

    java實現(xiàn)RedisTemplate操作哈希數(shù)據(jù)

    RedisTemplate是Spring Data Redis提供的一個用于操作Redis的模板類,本文主要介紹了java實現(xiàn)RedisTemplate操作哈希數(shù)據(jù),具有一定的參考價值,感興趣的可以了解一下
    2024-09-09
  • Java 中Object的wait() notify() notifyAll()方法使用

    Java 中Object的wait() notify() notifyAll()方法使用

    這篇文章主要介紹了Java 中Object的wait() notify() notifyAll()方法使用的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Spring框架AOP基礎(chǔ)之代理模式詳解

    Spring框架AOP基礎(chǔ)之代理模式詳解

    代理模式(Proxy Parttern)為一個對象提供一個替身,來控制這個對象的訪問,即通過代理對象來訪問目標對象。本文將通過示例詳細講解一下這個模式,需要的可以參考一下
    2022-11-11
  • Springbean的幾種注入方式都了解嗎

    Springbean的幾種注入方式都了解嗎

    這篇文章主要介紹了Springbean的幾種注入方式都了解嗎,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • Java org.w3c.dom.Document 類方法引用報錯

    Java org.w3c.dom.Document 類方法引用報錯

    這篇文章主要介紹了Java org.w3c.dom.Document 類方法引用報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • spring boot2.0總結(jié)介紹

    spring boot2.0總結(jié)介紹

    今天小編就為大家分享一篇關(guān)于spring boot2.0總結(jié)介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Spring控制Bean加載順序的操作方法

    Spring控制Bean加載順序的操作方法

    正常情況下,Spring 容器加載 Bean 的順序是不確定的,那么我們?nèi)绻枰错樞蚣虞d Bean 時應(yīng)如何操作?本文將詳細講述我們?nèi)绾尾拍芸刂?nbsp;Bean 的加載順序,需要的朋友可以參考下
    2024-05-05
  • 解析Idea為什么不推薦使用@Autowired進行Field注入

    解析Idea為什么不推薦使用@Autowired進行Field注入

    這篇文章主要介紹了Idea不推薦使用@Autowired進行Field注入的原因,網(wǎng)上文章大部分都是介紹兩者的區(qū)別,沒有提到為什么,當時想了好久想出了可能的原因,今天來總結(jié)一下
    2022-05-05
  • Mybatis查詢Sql結(jié)果未映射到對應(yīng)得實體類上的問題解決

    Mybatis查詢Sql結(jié)果未映射到對應(yīng)得實體類上的問題解決

    使用mybatis查詢表數(shù)據(jù)得時候,發(fā)現(xiàn)對應(yīng)得實體類字段好多都是null,本文主要介紹了Mybatis查詢Sql結(jié)果未映射到對應(yīng)得實體類上的問題解決,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • java分布式面試接口如何保證冪等及概念理解

    java分布式面試接口如何保證冪等及概念理解

    這篇文章主要為大家介紹了java分布式面試中接口如何保證冪等的問題解答以及概念描述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-03-03

最新評論

祁连县| 贵州省| 金阳县| 河间市| 阳东县| 静安区| 满城县| 铁力市| 寿光市| 汤原县| 肇东市| 石首市| 耿马| 卢湾区| 滨海县| 灌阳县| 屏东县| 朝阳市| 松滋市| 友谊县| 略阳县| 论坛| 台东市| 广宁县| 威远县| 屏山县| 福建省| 宽甸| 景东| 白城市| 广宁县| 且末县| 康马县| 石首市| 姚安县| 扶沟县| 灯塔市| 五华县| 安龙县| 邳州市| 大余县|