Office文档在线预览

谁践踏了优雅 2022-04-24 01:02 738阅读 0赞

文章目录

    • 方案一:word转html
          1. 添加依赖
          1. 编写工具类Word2HtmlUtil.java
          1. 测试
    • 方案二:使用第三方服务
    • 方案三:使用微软 Office Online 服务
    • 永中文档预览服务详解
          1. 试一试在线预览!
          1. Web调用
          1. Java调用

方案一:word转html

使用Apache POI将word转为html,生成静态html,预览功能直接链接到html。

1. 添加依赖

  1. <!-- word转html -->
  2. <dependency>
  3. <groupId>fr.opensagres.xdocreport</groupId>
  4. <artifactId>fr.opensagres.xdocreport.document</artifactId>
  5. <version>1.0.5</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>fr.opensagres.xdocreport</groupId>
  9. <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
  10. <version>1.0.5</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.poi</groupId>
  14. <artifactId>poi</artifactId>
  15. <version>3.12</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.poi</groupId>
  19. <artifactId>poi-scratchpad</artifactId>
  20. <version>3.12</version>
  21. </dependency>

2. 编写工具类Word2HtmlUtil.java

  1. import java.io.BufferedWriter;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.OutputStreamWriter;
  11. import java.util.List;
  12. import javax.xml.parsers.DocumentBuilderFactory;
  13. import javax.xml.transform.OutputKeys;
  14. import javax.xml.transform.Transformer;
  15. import javax.xml.transform.TransformerFactory;
  16. import javax.xml.transform.dom.DOMSource;
  17. import javax.xml.transform.stream.StreamResult;
  18. import org.apache.poi.hwpf.HWPFDocument;
  19. import org.apache.poi.hwpf.converter.PicturesManager;
  20. import org.apache.poi.hwpf.converter.WordToHtmlConverter;
  21. import org.apache.poi.hwpf.usermodel.Picture;
  22. import org.apache.poi.hwpf.usermodel.PictureType;
  23. import org.apache.poi.xwpf.converter.core.BasicURIResolver;
  24. import org.apache.poi.xwpf.converter.core.FileImageExtractor;
  25. import org.apache.poi.xwpf.converter.core.FileURIResolver;
  26. import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
  27. import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
  28. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  29. import org.w3c.dom.Document;
  30. public class Word2HtmlUtil {
  31. /** * 兼容doc和docx的word转html方法 * @param inFile 需要转换的word文件 * @param outPath 输出文件路径 * @param outName 输出文件名 */
  32. public static void word2Html(String inFile, String outPath, String outName) throws Exception{
  33. FileInputStream fis=new FileInputStream(inFile);
  34. String suffix=inFile.substring(inFile.lastIndexOf("."));
  35. if (suffix.equalsIgnoreCase(".docx")) {
  36. docx2Html(fis, outPath, outName);
  37. } else {
  38. doc2Html(fis, outPath, outName);
  39. }
  40. }
  41. public static void docx2Html(InputStream fis, String outPath ,String outName) throws Exception{
  42. // 加载word文档生成 XWPFDocument对象
  43. XWPFDocument document = new XWPFDocument(fis);
  44. // 解析 XHTML配置
  45. String imageFolder = outPath + "images"+File.separator+"docx"+File.separator;//图片存放路径
  46. File imageFolderFile = new File(imageFolder);
  47. XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
  48. options.setExtractor(new FileImageExtractor(imageFolderFile));
  49. options.setIgnoreStylesIfUnused(false);
  50. options.setFragment(true);
  51. options.URIResolver(new BasicURIResolver("images/docx/"));//html中img的src前缀
  52. // 将 XWPFDocument转换成XHTML
  53. OutputStream out = new FileOutputStream(new File(outPath + outName));
  54. XHTMLConverter.getInstance().convert(document, out, options);
  55. }
  56. public static void doc2Html(InputStream fis, String outPath, String outName) throws Exception {
  57. HWPFDocument wordDocument = new HWPFDocument(fis);
  58. WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
  59. wordToHtmlConverter.setPicturesManager(new PicturesManager() {
  60. public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
  61. return "images/doc/" + suggestedName;// html中img的src值
  62. }
  63. });
  64. wordToHtmlConverter.processDocument(wordDocument);
  65. //保存图片
  66. List <Picture> pics = wordDocument.getPicturesTable().getAllPictures();
  67. if (pics != null) {
  68. for (int i = 0; i < pics.size(); i++) {
  69. Picture pic = (Picture) pics.get(i);
  70. try {
  71. String imageFolder = outPath + "images" + File.separator + "doc" + File.separator;
  72. File dir=new File(imageFolder);//图片保存路径
  73. if(!dir.exists()) {
  74. dir.mkdirs();
  75. }
  76. pic.writeImageContent(new FileOutputStream(imageFolder + pic.suggestFullFileName()));
  77. } catch (FileNotFoundException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. Document htmlDocument = wordToHtmlConverter.getDocument();
  83. ByteArrayOutputStream out = new ByteArrayOutputStream();
  84. DOMSource domSource = new DOMSource(htmlDocument);
  85. StreamResult streamResult = new StreamResult(out);
  86. TransformerFactory tf = TransformerFactory.newInstance();
  87. Transformer serializer = tf.newTransformer();
  88. serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
  89. serializer.setOutputProperty(OutputKeys.INDENT, "yes");
  90. serializer.setOutputProperty(OutputKeys.METHOD, "html");
  91. serializer.transform(domSource, streamResult);
  92. out.close();
  93. writeFile(new String(out.toByteArray()), outPath + outName);
  94. }
  95. public static void writeFile(String content, String path) {
  96. FileOutputStream fos = null;
  97. BufferedWriter bw = null;
  98. try {
  99. File file = new File(path);
  100. fos = new FileOutputStream(file);
  101. bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
  102. bw.write(content);
  103. } catch (FileNotFoundException fnfe) {
  104. fnfe.printStackTrace();
  105. } catch (IOException ioe) {
  106. ioe.printStackTrace();
  107. } finally {
  108. try {
  109. if (bw != null)
  110. bw.close();
  111. if (fos != null)
  112. fos.close();
  113. } catch (IOException ie) { }
  114. }
  115. }
  116. }

3. 测试

  1. @Test
  2. public void testWord2Html() throws Exception{
  3. Word2HtmlUtil.word2Html("D:/test/doctest.doc", "D:/test/output/","doctest.html");
  4. }

在这里插入图片描述

方案二:使用第三方服务

一般是收费的,一般也会提供功能受限的免费版本。

  • 永中 http://www.yozodcs.com/index.html (有免费版)
  • Office Web 365 http://www.officeweb365.com/ (有免费版)
  • I Doc View http://www.officeweb365.com/

方案三:使用微软 Office Online 服务

地址:https://products.office.com/zh-CN/office-online/view-office-documents-online

使用:在https://view.officeapps.live.com/op/view.aspx?src=后连接在线文档URL(必须为 http:// 或 https:// 形式,文档必须是 Word、Excel 或 PowerPoint 文档)。
测试:https://view.officeapps.live.com/op/view.aspx?src=http%3A%2F%2Fwww.snpdp.com%2Ffile-download-1064.html

163邮箱附件预览(高清版)就是使用office apps实现的:
在这里插入图片描述


永中文档预览服务详解

163邮箱附件预览(极速版)就是使用永中的服务实现的:在这里插入图片描述

说明:
1.使用该方法,文档应该会被上传到永中的服务器,非公开文档不宜使用(购买的话应该也可以部署的自己的服务器上,不深究)。
2.下面测试方法中上传的文档不知道会不会被定时清理而不可用,不过如果有这个问题,只要申请免费受限账号应该就可以了。

1. 试一试在线预览!

地址:http://www.yozodcs.com/page/example.html
测试:http://www.yozodcs.com:8000/2019/04/11/MTkwNDExNTgxODc5MDUx.html
在这里插入图片描述

2. Web调用

(1)URL文档

  1. $.ajax({
  2. url: "http://dcs.yozosoft.com:80/onlinefile", //服务地址
  3. data: {
  4. downloadUrl: "http://www.snpdp.com/file-download-1064.html", //要预览的文档地址
  5. convertType: "0"
  6. },
  7. dataType: "json",
  8. type: "post",
  9. success: function(data) {
  10. console.log(data);
  11. //window.location=data.data[0];
  12. },
  13. error: function(data) {
  14. console.error(data)
  15. }
  16. });

接口说明、convertType参数取值说明、返回参数说明:http://www.yozodcs.com/help.html#link14

(2)上传文档
引入ajaxfileupload.js(jQuery-File-Upload)

  1. <input id="fileupload" type="file" name="file"><!-- name值需为file,与永中接口参数名对应 -->
  2. <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  3. <script src="js/jquery.ui.widget.js"></script>
  4. <script src="js/jquery.fileupload.js"></script>
  5. <script> $(function() { $('#fileupload').fileupload({ url: 'http://dcs.yozosoft.com:80/upload', dataType: 'json', formData: { convertType: "0" }, done: function(e, data) { console.log(data.result); } }); }); </script>

测试:http://dcs.yozosoft.com:8000/2019/04/12/MTkwNDEyNDAyNDM2MzMx.html
在这里插入图片描述

3. Java调用

(1)添加依赖

  1. <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  2. <dependency>
  3. <groupId>commons-logging</groupId>
  4. <artifactId>commons-logging</artifactId>
  5. <version>1.2</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
  8. <dependency>
  9. <groupId>org.apache.httpcomponents</groupId>
  10. <artifactId>httpclient</artifactId>
  11. <version>4.5.8</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
  14. <dependency>
  15. <groupId>org.apache.httpcomponents</groupId>
  16. <artifactId>httpcore</artifactId>
  17. <version>4.4.11</version>
  18. </dependency>
  19. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
  20. <dependency>
  21. <groupId>org.apache.httpcomponents</groupId>
  22. <artifactId>httpmime</artifactId>
  23. <version>4.5.8</version>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  26. <dependency>
  27. <groupId>com.alibaba</groupId>
  28. <artifactId>fastjson</artifactId>
  29. <version>1.2.55</version>
  30. </dependency>

(2)编写工具类DCSUtil.java

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.nio.charset.Charset;
  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.HttpStatus;
  12. import org.apache.http.ParseException;
  13. import org.apache.http.client.HttpClient;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.entity.mime.HttpMultipartMode;
  16. import org.apache.http.entity.mime.MultipartEntity;
  17. import org.apache.http.entity.mime.content.FileBody;
  18. import org.apache.http.entity.mime.content.StringBody;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import org.apache.http.util.EntityUtils;
  21. /** * @Description: DCS文档转换服务Java调用代码示例 */
  22. public class DCSUtil {
  23. /** * 向指定 URL 发送POST方法的请求 * @param url 发送请求的 URL * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */
  24. public static String sendPost(String url, String param) {
  25. PrintWriter out = null;
  26. BufferedReader in = null;
  27. String result = "";
  28. try {
  29. URL realUrl = new URL(url);
  30. // 打开和URL之间的连接
  31. URLConnection conn = realUrl.openConnection();
  32. conn.setRequestProperty("Accept-Charset", "UTF-8");
  33. // 设置通用的请求属性
  34. conn.setRequestProperty("accept", "*/*");
  35. conn.setRequestProperty("connection", "Keep-Alive");
  36. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  37. // 发送POST请求必须设置如下两行
  38. conn.setDoOutput(true);
  39. conn.setDoInput(true);
  40. // 获取URLConnection对象对应的输出流
  41. out = new PrintWriter(conn.getOutputStream());
  42. // 发送请求参数
  43. out.print(param);
  44. // flush输出流的缓冲
  45. out.flush();
  46. // 定义BufferedReader输入流来读取URL的响应
  47. in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  48. String line;
  49. while ((line = in.readLine()) != null) {
  50. result += line;
  51. }
  52. } catch (Exception e) {
  53. System.out.println("发送 POST 请求出现异常!" + e);
  54. e.printStackTrace();
  55. }
  56. // 使用finally块来关闭输出流、输入流
  57. finally {
  58. try {
  59. if (out != null) {
  60. out.close();
  61. }
  62. if (in != null) {
  63. in.close();
  64. }
  65. } catch (IOException ex) {
  66. ex.printStackTrace();
  67. }
  68. }
  69. return result;
  70. }
  71. /** * 向指定 URL 上传文件POST方法的请求 * * @param url 发送请求的 URL * @param filepath 文件路径 * @param type 转换类型 * @return 所代表远程资源的响应结果, json数据 */
  72. public static String SubmitPost(String url, String filepath, String type) {
  73. String requestJson = "";
  74. HttpClient httpclient = new DefaultHttpClient();
  75. try {
  76. HttpPost httppost = new HttpPost(url);
  77. FileBody file = new FileBody(new File(filepath));
  78. MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
  79. Charset.forName("UTF-8"));
  80. reqEntity.addPart("file", file); // file为请求后台的File upload;属性
  81. reqEntity.addPart("convertType", new StringBody(type, Charset.forName("UTF-8")));
  82. httppost.setEntity(reqEntity);
  83. HttpResponse response = httpclient.execute(httppost);
  84. int statusCode = response.getStatusLine().getStatusCode();
  85. if (statusCode == HttpStatus.SC_OK) {
  86. HttpEntity resEntity = response.getEntity();
  87. requestJson = EntityUtils.toString(resEntity);
  88. EntityUtils.consume(resEntity);
  89. }
  90. } catch (ParseException e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. // requestJson = e.toString();
  94. } catch (IOException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. // requestJson = e.toString();
  98. } finally {
  99. try {
  100. httpclient.getConnectionManager().shutdown();
  101. } catch (Exception ignore) {
  102. }
  103. }
  104. return requestJson;
  105. }
  106. }

(3)测试

  1. @Test
  2. public void testDCS() {
  3. // 文件上传转换
  4. String convertByFile = DCSUtil.SubmitPost("http://dcs.yozosoft.com:80/upload", "D:/test/doctest.doc", "0");
  5. System.out.println(convertByFile);
  6. // 输出:{"result":0,"data":["http://dcs.yozosoft.com:8000/2019/04/12/MTkwNDEyNTM5NTE2Njk5.html"],"message":"转换成功","type":0}
  7. // 网络地址转换
  8. String convertByUrl = DCSUtil.sendPost("http://dcs.yozosoft.com:80/onlinefile",
  9. "downloadUrl=http://www.snpdp.com/file-download-1064.html&convertType=1");
  10. System.out.println(convertByUrl);
  11. // 输出:{"result":0,"data":["http://dcs.yozosoft.com:8000/2019/04/12/MTkwNDEyNTQ0NjcyMDI3.html"],"message":"转换成功","type":1}
  12. }

发表评论

表情:
评论列表 (有 0 条评论,738人围观)

还没有评论,来说两句吧...

相关阅读