Realize the preview function similar to Baidu online disk: support Word, Excel and Ppt

Keywords: Java Excel network JSON

I'm a technical Xiaobai. The boss asked me to write a function of office online preview a few days ago. It's similar to Baidu online disk. I find all kinds of materials and summarize the methods I think are better to use.

 

There are probably several ways to preview office online.

 

The first kind

 

Using office online to realize online preview, the Office platform provides the preview effect through the direction of url.

 

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

 

The following src fills in the address of the document to be uploaded to the server. The address needs to be compiled by using URLEncode.

 

Example:

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

 

Reference link:

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

 

Advantage: this feature is provided by Office so you only need to splice addresses.

 

Disadvantages: the access address of the document cannot be directly ip, and it needs to be accessed through the domain name, and the port must be port 80, the size of Word and ppt documents cannot exceed 10M, and Excel cannot exceed 5M

 

Second kinds of species

 

1. Use the third-party tool openoffice to convert word, excel, ppt, txt and other files into pdf files,

 

2. Convert pdf file to swf file by swfTools

 

3. Display on the page through FlexPaper document component.  

 

Reference link:

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

 

Summarize the advantages and disadvantages of this method

 

Advantages: openoffice has various versions, supports cross platform and fast conversion;

 

Disadvantages: there will be defects in the style of conversion. Excel and openoffice are perfectly incompatible =. =. When converting, there will be folding, segmentation and other situations, and the effect is not ideal;

 

FlexPaper is displayed with Flash plug-in, showing the advantages of low.

 

Third kinds of species

 

Using poi to realize office online preview

 

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
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")) {      //Read document content      InputStream in = new FileInputStream(f);      XWPFDocument document = new XWPFDocument(in);      File imageFolderFile = new File(path);      //Picture path when loading html page      XHTMLOptions options = XHTMLOptions.create().URIResolver( new BasicURIResolver("./"));      //Picture save folder path      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");    }  }}

 

Summarize the advantages and disadvantages of this method

 

Advantages: pure java code does not need third-party software, poi for Excel analysis is better, can be perfectly converted.

 

Disadvantages: the jar package is really hard to find. The incompatibilities of various versions, such as. doc and docx generated by word03 and word07 respectively. poi doesn't recognize. doc, and then various errors are reported. Because Html is generated, it will store the pictures in Word in a single file, which takes up a lot of space.

 

Fourth kinds of species

 

Using jacob to realize the function of office conversion pdf to achieve preview.

 

Reference in pom file

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
<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>

Find the corresponding version package of Jacob on the Internet, copy jacob-1.14.3-x64.dll to the directory Java\jdk1.8.0\jre\bin,

 

After many tests, we found that jacob had some problems with Excel conversion, so I used poi for Excel conversion.

 

Customize a tool class 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;// Pending changes are not saved.    static final int wdFormatPDF = 17;// word to PDF    static final int ppSaveAsPDF = 32;// ppt to PDF

    public static void main(String[] args) throws IOException {
    }

    public void word2pdf(String wordPath,String target,String wordName,String suffix){

        System.out.println("start-up 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("open documents" + source);            Dispatch doc = Dispatch.call(docs,//                    "Open", //                    source,// FileName                    false,// ConfirmConversions                    true // ReadOnly            ).toDispatch();            System.out.println("Convert document to PDF " + target);            File tofile = new File(target);            //Verify that the destination path exists            if (tofile.exists()) {                System.out.println("existence");            }            Dispatch.call(doc,//                    "SaveAs", //                    target, // FileName                    wdFormatPDF);

            Dispatch.call(doc, "Close", false);            long end = System.currentTimeMillis();            System.out.println("convert network..When used:" + (end - start) + "ms.");//            Cut.cutfile(str_s,str_d);        } catch (Exception e) {            System.out.println("========Error:Document conversion failed:" + e.getMessage());        } finally {            if (app != null)                app.invoke("Quit", wdDoNotSaveChanges);        }    }

    public void ppt2pdf(String wordPath,String target,String wordName,String suffix){        System.out.println("start-up 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("open documents" + source);            Dispatch presentation = Dispatch.call(presentations,//                    "Open",                    source,// FileName                    true,// ReadOnly                    true,// Untitled specifies whether the file has a title.                    false // WithWindow specifies whether the file is visible.            ).toDispatch();

            System.out.println("Convert document to 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("convert network..When used:" + (end - start) + "ms.");

        } catch (Exception e) {            System.out.println("========Error:Document conversion failed:" + e.getMessage());        } finally {            if (app != null) app.invoke("Quit");        }    }}

 

Define another class ExcelToHtml of poi conversion Html

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
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() {    }

    /**     * Because the Html page converted by poi will appear garbled when it is displayed in the browser, please force the conversion here     * @param filePath     */    public void html2utf(String filePath) {        try {            String content = "charset=utf-8";            String templateContent = "";            FileInputStream fileinputstream = new FileInputStream(filePath);// Read template file            // Here are four lines: get the length of the input stream, build an array of that length, read the data in the input stream into the array in bytes, and then close the stream            int lenght = fileinputstream.available();            byte bytes[] = new byte[lenght];            fileinputstream.read(bytes);            fileinputstream.close();            // By using the default character set to decode the specified byte array, a new            //Because the original html file is in gb2312 format, it should also be read in GBK format when it is converted to string, otherwise it is still garbled            templateContent = new String(bytes, "GBK");            templateContent = templateContent.replaceFirst("charset=gb2312", content);            // Because the String has been replaced, the UTF-8 character set is used to encode the String as a byte sequence and store the result in a new byte array.            byte tag_bytes[] = templateContent.getBytes("UTF-8");            FileOutputStream fileoutputstream = new FileOutputStream(filePath);// Create a file output stream            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;    }

}

 

The main two classes have been completed, because this is only a small demo, so the code may be a little less standardized

 

controller class

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
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";//        Upload to the temporary folder of / 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")) {            //Some browsers will compile the Chinese in html name, resulting in 404, so I use time stamp instead of my name            //original file name            String original_name = fileName;            //Changed name            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");    }}

 

Foreground page

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
<%@ 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("Please upload the file first");            }            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("Upload success");                }, error:function(e){                    alert(e.error);                    window.clearInterval(timer);                }            })        }</script></head><body><form class="form" enctype="multipart/form-data" id="form">    Select file:<input type="file" name="file" width="180px">    <input type="button" value="upload" οnclick="test()">    <input type="button" value="preview" οnclick="load()"></form><iframe src="" id="iframe" width="100%" height="650px"/></body></html>

 

Summarize the advantages and disadvantages of jacob

 

Advantages: compared with other methods, it can be well converted.

 

Disadvantages: the conversion efficiency is very low. Generally, a Word needs about 4s. A larger file is very slow. It does not support cross platform, and can only run under windows.

633 original articles published, 326 praised, 300000 visitors+

Posted by Yves on Mon, 13 Jan 2020 05:25:32 -0800