网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java通过jacob实现office在线预览功能

简介:

成都创新互联是一家专业提供丰镇企业网站建设,专注与网站设计、网站建设、HTML5建站、小程序制作等业务。10年已为丰镇众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

这篇文章中的代码都是参考于网上的,只做一个记录。主要做的就是实现一个office在线预览功能。

第一步:装office

第二步:下载jacob

打开网址下载,目前最新的是1.19版本。

第三步:配置jdk

解压下载完的jacob压缩包,根据jdk的版本选择dll中的一个,放入/jdk/jre/bin中。

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

在maven官网上找不到com.jacob的jar包,只能手动引入,这个jar包在jacob的压缩包中有。


 com.jacob
 jacob
 1.19
 system
 ${project.basedir}/lib/jacob.jar

第五步:将office转化为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 path3 = "C:\\Users\\acer\\Desktop\\测试.pdf";
 word2PDF(path, path3);
 String path4 = "C:\\Users\\acer\\Desktop\\测试2.ppt";
 String path5 = "C:\\Users\\acer\\Desktop\\测试2.pdf";
 ppt2PDF(path4, path5);
 String path6 = "C:\\Users\\acer\\Desktop\\测试3.xls";
 String path7 = "C:\\Users\\acer\\Desktop\\测试3.pdf";
 excel2PDF(path6, path7);
 }

 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) {
 //从数据库中查出文件位置和文件名字
 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();
 }
 }

前端:




 
 pdf在线预览


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享文章:java通过jacob实现office在线预览功能
分享地址:http://bjjierui.cn/article/jijoco.html

其他资讯