本人技术小白一枚,前些天老大让我写一个office在线预览的一个功能,跟百度网盘类似,各种找资料,总结一下我觉得比较好用的方法。

网上大概有这几种office在线预览的方法。

# 第一种

利用office online实现在线预览,Office平台提供了通过url的指向达到预览效果。

http://view.officeapps.live.com/op/view.aspx?src=

后面的src填写文档上传到服务器的地址,地址需要通过用URLEncode进行编译。

例:

http://view.officeapps.live.com/op/view.aspx?src=http%3a%2f%2fvideo.ch9.ms%2fbuild%2f2011%2fslides%2fTOOL-532T_Sutter.pptx

参照链接:

https://blog.csdn.net/csdn_cjgu/article/details/69389474

优点:这个功能由Office提供所以只需要拼接地址。

缺点:文档的访问地址不能直接使用 ip,需要通过域名访问,并且端口必须是 80 端口,Word、ppt文档的大小不能超过10M,Excel不能超过5M

# 第二种

1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件,

2.通过swfTools将pdf文件转换成swf格式的文件;

3.通过FlexPaper文档组件在页面上进行展示。

参照链接:

https://blog.csdn.net/tmac937436/article/details/70799687?locationNum=14&fps=1

总结一下这种方法的利弊

优点:openoffice有各种版本,支持跨平台,转换速度快;

缺点:转换的样式会有缺损,Excel和openoffice完美不兼容 =  . = ,转换的时候会出现折行,切分等情况,效果很不理想;

FlexPaper这个是用Flash插件显示的,展示的效果优点low。

# 第三种

利用poi实现office在线预览

public static void PoiWord07ToHtml (HttpServletRequest request) throws IOException{  String path= "C:\\Users\\Administrator\\Desktop\\";  String file = "C:\\Users\\Administrator\\Desktop\\word07.docx";  String file2 ="C:\\Users\\Administrator\\Desktop\\word07.html";  File f = new File(file);  if (!f.exists()) {    System.out.println("Sorry File does not Exists!");  } else {    if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) {      //读取文档内容      InputStream in = new FileInputStream(f);      XWPFDocument document = new XWPFDocument(in);      File imageFolderFile = new File(path);      //加载html页面时图片路径      XHTMLOptions options = XHTMLOptions.create().URIResolver( new BasicURIResolver("./"));      //图片保存文件夹路径      options.setExtractor(new FileImageExtractor(imageFolderFile));      OutputStream out = new FileOutputStream(new File(file2));      XHTMLConverter.getInstance().convert(document, out, options);      out.close();    } else {      System.out.println("Enter only MS Office 2007+ files");    }  }}

总结一下这种方法的利弊

优点:纯java代码不需要第三方软件,poi对于Excel的解析比较好,可以完美转换。

缺点:jar包真的好难找啊,各种版本不兼容问题,比如word03和word07生成的分别是.doc和docx,poi对于.doc不识别,然后各种报错,由于生成的是Html所以它会把Word中的图片单独存放在一个文件下很占空间。

# 第四种

使用jacob实现office转换pdf达到预览功能。

pom文件中引用

<dependency>  <groupId>net.sf.jacob-project</groupId>  <artifactId>jacob</artifactId>  <version>1.14.3</version></dependency><dependency>  <groupId>com.lowagie</groupId>  <artifactId>itext</artifactId>  <version>2.1.7</version></dependency><!--poi--><dependency>  <groupId>org.apache.poi</groupId>  <artifactId>poi-ooxml</artifactId>  <version>3.14-beta1</version></dependency>

网上查找jacob对应版本包把jacob-1.14.3-x64.dll复制到需要在Java\jdk1.8.0\jre\bin目录下,

经过多次测试发现jacob对于Excel的转换有些问题,所以Excel的转换我用的是poi。

自定义一个工具类Word2Pdf

import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;import java.io.*;public class Word2Pdf {    static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。    static final int wdFormatPDF = 17;// word转PDF 格式    static final int ppSaveAsPDF = 32;// ppt 转PDF 格式    public static void main(String[] args) throws IOException {
    }    public void word2pdf(String wordPath,String target,String wordName,String suffix){        System.out.println("启动Word");        long start = System.currentTimeMillis();        String source = wordPath + wordName + suffix;        target = target + wordName ;        ActiveXComponent app = null;        try {            app = new ActiveXComponent("Word.Application");            app.setProperty("Visible", false);            app.setProperty("DisplayAlerts", new Variant(false));            Dispatch docs = app.getProperty("Documents").toDispatch();            System.out.println("打开文档" + source);            Dispatch doc = Dispatch.call(docs,//                    "Open", //                    source,// FileName                    false,// ConfirmConversions                    true // ReadOnly            ).toDispatch();            System.out.println("转换文档到PDF " + target);            File tofile = new File(target);            //验证目标路径是否存在            if (tofile.exists()) {                System.out.println("存在");            }            Dispatch.call(doc,//                    "SaveAs", //                    target, // FileName                    wdFormatPDF);            Dispatch.call(doc, "Close", false);            long end = System.currentTimeMillis();            System.out.println("转换完成..用时:" + (end - start) + "ms.");//            Cut.cutfile(str_s,str_d);        } catch (Exception e) {            System.out.println("========Error:文档转换失败:" + e.getMessage());        } finally {            if (app != null)                app.invoke("Quit", wdDoNotSaveChanges);        }    }    public void ppt2pdf(String wordPath,String target,String wordName,String suffix){        System.out.println("启动PPT");        String source = wordPath + wordName + suffix;        target = target + wordName ;        long start = System.currentTimeMillis();        ActiveXComponent app = null;        try {            app = new ActiveXComponent("Powerpoint.Application");            Dispatch presentations = app.getProperty("Presentations").toDispatch();            System.out.println("打开文档" + source);            Dispatch presentation = Dispatch.call(presentations,//                    "Open",                    source,// FileName                    true,// ReadOnly                    true,// Untitled 指定文件是否有标题。                    false // WithWindow 指定文件是否可见。            ).toDispatch();            System.out.println("转换文档到PDF " + target);            File tofile = new File(target);            if (tofile.exists()) {                tofile.delete();            }            Dispatch.call(presentation,//                    "SaveAs", //                    target, // FileName                    ppSaveAsPDF);            Dispatch.call(presentation, "Close");            long end = System.currentTimeMillis();            System.out.println("转换完成..用时:" + (end - start) + "ms.");        } catch (Exception e) {            System.out.println("========Error:文档转换失败:" + e.getMessage());        } finally {            if (app != null) app.invoke("Quit");        }    }}

再定义一个poi转换Html的一个类ExcelToHtml

import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;import java.io.FileInputStream;import java.io.FileOutputStream;public class ExcelToHtml {    private final static ExcelToHtml oOfficeToXML = new ExcelToHtml();    public static ExcelToHtml getInstance() {        return oOfficeToXML;    }    public ExcelToHtml() {    }    /**     * 因为poi转换的Html页面在浏览器中显示时会出现乱码问题,这里强制转换一下     * @param filePath     */    public void html2utf(String filePath) {        try {            String content = "charset=utf-8";            String templateContent = "";            FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件            // 下面四行:获得输入流的长度,然后建一个该长度的数组,然后把输入流中的数据以字节的形式读入到数组中,然后关闭流            int lenght = fileinputstream.available();            byte bytes[] = new byte[lenght];            fileinputstream.read(bytes);            fileinputstream.close();            // 通过使用默认字符集解码指定的 byte 数组,构造一个新的            //因为原来的html文件是gb2312格式,所以转为string时也要以GBK格式读取,不然依旧是乱码            templateContent = new String(bytes, "GBK");            templateContent = templateContent.replaceFirst("charset=gb2312", content);            // 因为已经替换字符串了,所以使用UTF-8字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。            byte tag_bytes[] = templateContent.getBytes("UTF-8");            FileOutputStream fileoutputstream = new FileOutputStream(filePath);// 建立文件输出流            fileoutputstream.write(tag_bytes);            fileoutputstream.close();        } catch (Exception e) {            System.out.print(e.toString());        }    }    public boolean ExceltoHtml(String wordPath,String original_name,String target,String wordName,String suffix) {        ComThread.InitSTA();        ActiveXComponent activexcomponent = new                ActiveXComponent("Excel.Application");        String source = wordPath + original_name + suffix;        target = target + wordName;        boolean flag = false;        try        {            activexcomponent.setProperty("Visible", new Variant(false));            Dispatch dispatch =                    activexcomponent.getProperty("Workbooks").toDispatch();            Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1, new                    Object[] {                    source, new Variant(false), new Variant(true)            }, new int[1]).toDispatch();            Dispatch.call(dispatch1, "SaveAs", target, new Variant(44));            Variant variant = new Variant(false);            Dispatch.call(dispatch1, "Close", variant);            flag = true;        }        catch(Exception exception)        {            System.out.println("|||" + exception.toString());        }        finally        {            activexcomponent.invoke("Quit", new Variant[0]);            ComThread.Release();            ComThread.quitMainSTA();        }        return flag;    }}

主要的两个类已完成,因为这只是一个小demo所以代码可能有些不太规范

controller类

import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.servlet.ModelAndView;import pojo.UpFile;import tools.ExcelToHtml;import tools.Word2Pdf;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;@Controller@RequestMapping("/sys")public class controller {
    Word2Pdf word2Pdf = new Word2Pdf();    @RequestMapping("/index")    public ModelAndView index(){        ModelAndView modelAndView = new ModelAndView();//        Word2Pdf word2Pdf = new Word2Pdf();//        word2Pdf.word2pdf("G:\\Test\\","G:\\Test\\PDF\\","",".doc");//        modelAndView.setViewName("index");        return modelAndView;    }    @RequestMapping(value="/upload.json",method=RequestMethod.POST)    @ResponseBody    public Map<Object,Object> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request)  throws Exception  {        UpFile upFile = new UpFile();        Map<Object,Object> map = new HashMap<Object, Object>();        String suffix = file.getOriginalFilename().substring(                file.getOriginalFilename().lastIndexOf("."));        String fileName = file.getOriginalFilename().substring(0,                file.getOriginalFilename().lastIndexOf("."));        String time = new SimpleDateFormat("yyyyMMddHHmmssSSS") .format(new Date() );        String filePath = request.getSession().getServletContext().getRealPath("upload/");        String target = "G:\\Test_02\\target\\backend\\statics\\pdf\\";//        String FileName = "\\statics\\pdf\\"+fileName+".pdf";        String FileName = "/statics/pdf/"+fileName+".pdf";//        上传到/target/backend/upload/的暂存文件夹下        File dir = new File(filePath,fileName+suffix);        file.transferTo(dir);        if (suffix.equals(".doc")||suffix.equals(".docx")){            WordToPdf(filePath,target,fileName,suffix);        } else if (suffix.equals(".ppt")||suffix.equals(".pptx")){            PptToPdf(filePath,target,fileName,suffix);        } else if (suffix.equals(".xls")||suffix.equals(".xlsx")) {            //有些浏览器会把html名字中的中文进行编译,导致404 所以我名字我就用时间戳代替了            //原文件名            String original_name = fileName;            //更改后的名字            String changeName = "Html" + time;            ExcelToHtml(filePath, original_name, changeName, suffix);            FileName = "\\statics\\Html\\" + changeName + ".htm";        }        map.put("filename",FileName);        return map;    }    public void WordToPdf(String filePath,String target,String fileName,String suffix){        word2Pdf.word2pdf(filePath,target,fileName,suffix);    }    public void PptToPdf(String filePath,String target,String wordName,String suffix){        word2Pdf.ppt2pdf(filePath,target,wordName,suffix);    }    public void ExcelToHtml(String filePath,String original_name,String wordName,String suffix){        ExcelToHtml excelToHtml = new ExcelToHtml();        excelToHtml.ExceltoHtml(filePath,original_name,"G:\\Test_02\\target\\backend\\statics\\Html\\",wordName,suffix);        excelToHtml.html2utf("G:\\Test_02\\target\\backend\\statics\\Html\\"+wordName+".htm");    }}

前台页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script type="text/javascript" src="/statics/jquery-3.2.1.min.js"></script>    <script type="text/javascript">        var filePath = "";        function load(){            if (filePath==""||filePath.length==0){                alert("请先上传文件");            }            document.getElementById("iframe").src=filePath        }        function test(){            var form = new FormData(document.getElementById("form"));            $.ajax({                url:"/sys/upload.json",                type:"post",                data:form,                cache: false,                processData: false,                contentType: false,                success:function(data){                    filePath = (data.filename);                    alert("上传成功");                }, error:function(e){                    alert(e.error);                    window.clearInterval(timer);                }            })        }</script></head><body><form class="form" enctype="multipart/form-data" id="form">    选择文件:<input type="file" name="file" width="180px">    <input type="button" value="上传" οnclick="test()">    <input type="button" value="预览" οnclick="load()"></form><iframe src="" id="iframe" width="100%" height="650px"/></body></html>

总结一下jacob的利弊

优点:相对于其他方法可以较为完好的转换。

缺点:转换效率很低,通常一个Word需要4s左右,大一点的文件就很慢了,不支持跨平台,只能在windows下运行。

实现一下类似百度网盘的预览功能:支持Word、Excel、Ppt相关推荐

  1. office 文档 在线预览功能实现(word,excel,pdf,ppt等多种格式)——使用https://view.xdocin.com/view 提示文档过期——基础积累

    web实现office文档在线预览功能--基础积累 最近遇到一个需求,就是要实现多种文档链接的在线预览,最简单的方式就是通过window.open(url地址)的方式来实现. 但是如果要求是在一个弹窗 ...

  2. qstandarditemmodel 重写data函数后无法实现拖拽_实现类似百度网盘上传的功能

    今天写到了上传文件部分,记录一下. 前端使用了dropzone.js(用于实现文件上传的JS库),并对此文件进行了细微修改. 说一下思路吧: 首先界面长这样: 点击上传文件,需要弹出一个对话框,对话框 ...

  3. 全局拖拽上传文件(类似百度网盘)的实现及踩坑

    记录下,开发需求是实现一个类似百度网盘全局拖拽的功能,兼容浏览器,IE0以上.实现思路是,采用原声的拖拽事件.监听window上的dragenter事件,拖拽目标节点是fixed罩住页面的dropZo ...

  4. 如何设计类似百度网盘的系统?-百度网盘的系统设计分析

    在本文中,我们将设计一个Google Drive服务!! (类似百度网盘,所以标题取百度网盘应该没毛病.事实上不可能这么简单,有时间可以讨论以下百度网盘的网络架构) 系统设计是软件工程中最重要和最令人 ...

  5. C#实现类似百度网盘、育网校园云盘在“我的电脑”磁盘驱动器

    我们在安装育网云盘或百度网盘后,一般在我的电脑中会看到类似如下的界面: 类似这样的快捷方式是如何实现的呢?下面为大家一一讲解实现思路 1.这里显示出来的磁盘快捷方式,实际上在注册表中有相对应的记录,开 ...

  6. C#实现类似百度网盘、360云盘在“我的电脑”磁盘驱动器

    我们在安装360云盘或百度网盘后,一般在我的电脑中会看到类似如下的界面: 类似这样的快捷方式是如何实现的呢?下面为大家一一讲解实现思路 1.这里显示出来的磁盘快捷方式,实际上在注册表中有相对应的记录, ...

  7. 多人上传文件公共网盘_百度网盘带来文件收集功能 可设置后最多向500人收集文件并转存...

    百度网盘最近已经为网页版和客户端版带来文件收集功能,即生成特定的文件生成链接邀请其他用户提交文件等. 文件收集功能并不新鲜不过百度网盘倒是目前才支持 ,  据蓝点网测试该百度网盘文件收集功能最多支持5 ...

  8. outlook2010客户端无法预览及保存word,excel问题

    outlook2010客户端遇到的EXCEL预览及保存问题 今天遇到了一个这样的问题,outlook2010打开以后其他的excel都可以打开预览及保存,这个excel无法预览既保存,经查是outlo ...

  9. 使用element-ui的el-image图片预览功能 支持图片下载

    项目场景: 工作中我们常用会到 vue+element,其中预览组件难免会涉及到下载功能,或者其它扩展功能,但是el-image 支持的只是 简单放大旋转功能,解决方法 要么 修改组件,要么 通过 j ...

最新文章

  1. JS作用域相关知识(#精)
  2. 程序员的疯狂:打工与创业的残酷区别
  3. 漫扯:从polling到Websocket(ZZ)
  4. python3精要(48)-os命令
  5. jpa关联映射(一)
  6. 1_数据分析—数据载入、导出和探索
  7. ubuntu未发现wifi适配器_Windows 10 9月更新频翻车,1903版本网络适配器又出bug
  8. TextView实现歌词同步《IT蓝豹》
  9. ExtJS ComboBox同时加载远程和本地数据
  10. 抓取Js动态生成数据且以滚动页面方式分页的网页
  11. 你干的是高档活儿还是Low逼活儿,就在一念之间
  12. 计算机桌面图片打不开显示内存不足,电脑上的windows图片查看器提示内存不足如何解决...
  13. Win10 51仿真器PZTracker驱动安装失败 未知设备
  14. cubieboard开发板简介
  15. Vue组织架构图组件
  16. 道一HTTP测试工具功能升级
  17. 修改cmd编码为UTF-8
  18. 我眼中的微软Azure:Microsoft Azure
  19. Python高级培训第一次作业(寒假)
  20. 后端传来map数据,前端的获取方式

热门文章

  1. Delphi 编译指令与说明
  2. 阿里巴巴集团告别 CTO,一个时代又落幕了
  3. java 多项目_Java开发小技巧(三):Maven多工程依赖项目
  4. 戴尔服务器型号720xd,Dell PowerEdge R720/R720xd服务器,各有侧重双子星
  5. 匿名函数的表达式可以有多个吗python_匿-匿的意思-匿什么意思-匿的读音-匿的解释-新华字典-秒查询...
  6. android图片处理的工具代码
  7. 十年工作经验和教训(转)
  8. 分享 使用普通笔记本电脑,DIY制作合成混音MTV视频,并上传分享的步骤
  9. 血浆p-Tau181在Tau沉积中的预测作用
  10. gprof输出内容解释