Java实现office(word,excel,ppt)文档在线预览 您所在的位置:网站首页 java在线预览openoffice Java实现office(word,excel,ppt)文档在线预览

Java实现office(word,excel,ppt)文档在线预览

2023-11-12 12:19| 来源: 网络整理| 查看: 265

想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览。

一、使用openoffice方式实现word预览

主要思路是:

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

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

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

我使用的工具版本:

openof:3.4.1

swfTools:1007

FlexPaper:这个关系不大,我随便下的一个。推荐使用1.5.1

JODConverter:需要jar包,如果是maven管理直接引用就可以

操作步骤:

1.office准备

下载openoffice:

Apache OpenOffice - Official Download

从过往文件,其他语言中找到中文版3.4.1的版本

下载后,解压缩,安装

然后找到安装目录下的program 文件夹

在目录下运行

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

如果运行失败,可能会有提示,那就加上 .\ 在运行试一下

 扫VX 领Java资料,前端,测试,python等等资料都有

这样openoffice的服务就开启了。

将flexpaper文件中的js文件夹(包含了flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,这三个js文件主要是预览swf文件的插件)拷贝至网站根目录;将FlexPaperViewer.swf拷贝至网站根目录下(该文件主要是用在网页中播放swf文件的播放器)

项目结构:

 

页面代码:

fileUpload.jsp

文档在线预览系统 body {margin-top:100px;background:#fff;font-family: Verdana, Tahoma;} a {color:#CE4614;} #msg-box {color: #CE4614; font-size:0.9em;text-align:center;} #msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px;} #msg-box .title {font-size:1.4em;font-weight:bold;margin:0 0 30px 0;} #msg-box .nav {margin-top:20px;} 请上传要处理的文件,过程可能需要几分钟,请稍候片刻。

docUploadConvertAction.jsp

Insert title here body {margin-top:100px;background:#fff;font-family: Verdana, Tahoma;} a {color:#CE4614;} #msg-box {color: #CE4614; font-size:0.9em;text-align:center;} #msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px;} #msg-box .title {font-size:1.4em;font-weight:bold;margin:0 0 30px 0;} #msg-box .nav {margin-top:20px;}

documentView.jsp

html, body { height:100%; } body { margin:0; padding:0; overflow:auto; } #flashContent { display:none; } 文档在线预览系统 var fp = new FlexPaperViewer( 'FlexPaperViewer', 'viewerPlaceHolder', { config : { SwfFile : escape(''), Scale : 0.6, ZoomTransition : 'easeOut', ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : true, FitWidthOnLoad : false, FullScreenAsMaxWindow : false, ProgressiveLoading : false, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'SinglePage', ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, localeChain: 'en_US' }});

转换类:

DocConverter.java

package filetest; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; /** * doc docx格式转换 */ public class DocConverter { private static final int environment = 1;// 环境 1:windows 2:linux private String fileString;// (只涉及pdf2swf路径问题) private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置 private String fileName; private File pdfFile; private File swfFile; private File docFile; public DocConverter(String fileString) { ini(fileString); } /** * 重新设置file * * @param fileString */ public void setFile(String fileString) { ini(fileString); } /** * 初始化 * * @param fileString */ private void ini(String fileString) { this.fileString = fileString; fileName = fileString.substring(0, fileString.lastIndexOf(".")); docFile = new File(fileString); pdfFile = new File(fileName + ".pdf"); swfFile = new File(fileName + ".swf"); } /** * 转为PDF * * @param file */ private void doc2pdf() throws Exception { if (docFile.exists()) { if (!pdfFile.exists()) { OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); // close the connection connection.disconnect(); System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****"); } catch (java.net.ConnectException e) { e.printStackTrace(); System.out.println("****swf转换器异常,openoffice服务未启动!****"); throw e; } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { e.printStackTrace(); System.out.println("****swf转换器异常,读取转换文件失败****"); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } } else { System.out.println("****已经转换为pdf,不需要再进行转化****"); } } else { System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****"); } } /** * 转换成 swf */ @SuppressWarnings("unused") private void pdf2swf() throws Exception { Runtime r = Runtime.getRuntime(); if (!swfFile.exists()) { if (pdfFile.exists()) { if (environment == 1) {// windows环境处理 try { Process p = r.exec("D:/pdf2swf/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9"); //Process p=new Process(); System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.out.print(loadStream(p.getInputStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (IOException e) { e.printStackTrace(); throw e; } } else if (environment == 2) {// linux环境处理 try { Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (Exception e) { e.printStackTrace(); throw e; } } } else { System.out.println("****pdf不存在,无法转换****"); } } else { System.out.println("****swf已经存在不需要转换****"); } } static String loadStream(InputStream in) throws IOException { int ptr = 0; in = new BufferedInputStream(in); StringBuffer buffer = new StringBuffer(); while ((ptr = in.read()) != -1) { buffer.append((char) ptr); } return buffer.toString(); } /** * 转换主方法 */ @SuppressWarnings("unused") public boolean conver() { if (swfFile.exists()) { System.out.println("****swf转换器开始工作,该文件已经转换为swf****"); return true; } if (environment == 1) { System.out.println("****swf转换器开始工作,当前设置运行环境windows****"); } else { System.out.println("****swf转换器开始工作,当前设置运行环境linux****"); } try { doc2pdf(); pdf2swf(); } catch (Exception e) { e.printStackTrace(); return false; } if (swfFile.exists()) { return true; } else { return false; } } /** * 返回文件路径 * * @param s */ public String getswfPath() { if (swfFile.exists()) { String tempString = swfFile.getPath(); tempString = tempString.replaceAll("\\\\", "/"); return tempString; } else { return ""; } } /** * 设置输出路径 */ public void setOutputPath(String outputPath) { this.outputPath = outputPath; if (!outputPath.equals("")) { String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf(".")); if (outputPath.charAt(outputPath.length()) == '/') { swfFile = new File(outputPath + realName + ".swf"); } else { swfFile = new File(outputPath + realName + ".swf"); } } } }

然后运行。

中间遇到的问题,存放上传的upload文件夹要创建。

存放pdf2swf的位置的代码有问题,不知道为什么我第一次运行失败后,修改为正确地址,然后重启重写了很多次还是报错地址错误,最后将推荐哦那个样的代码放到新的项目里却可以正常运行不报错。

运行结果:

 

二、利用jodconverter(基于OpenOffice服务)将文件转化为html格式或者pdf格式(不成熟):

 扫VX 领Java资料,前端,测试,python等等资料都有

package org.pan.poi.poi; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ConnectException; import java.text.SimpleDateFormat; import java.util.Date; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; /** * @包名 org.pan.poi.poi * @创建人 蒋文章 * @日期 2018/4/23 0023 * @时间 15:39 * @描述 * 利用jodconverter(基于OpenOffice服务)将文件(*.doc、*.docx、*.xls、*.ppt)转化为html格式或者pdf格式, * 使用前请检查OpenOffice服务是否已经开启, OpenOffice进程名称:soffice.exe | soffice.bin */ public class Doc2HtmlUtil { private static Doc2HtmlUtil doc2HtmlUtil; /** * 获取Doc2HtmlUtil实例 */ public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() { if (doc2HtmlUtil == null) { doc2HtmlUtil = new Doc2HtmlUtil(); } return doc2HtmlUtil; } /** * 转换文件成html * * @param fromFileInputStream: * @throws IOException */ public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String timesuffix = sdf.format(date); String docFileName = null; String htmFileName = null; if("doc".equals(type)){ docFileName = "doc_" + timesuffix + ".doc"; htmFileName = "doc_" + timesuffix + ".html"; }else if("docx".equals(type)){ docFileName = "docx_" + timesuffix + ".docx"; htmFileName = "docx_" + timesuffix + ".html"; }else if("xls".equals(type)){ docFileName = "xls_" + timesuffix + ".xls"; htmFileName = "xls_" + timesuffix + ".html"; }else if("ppt".equals(type)){ docFileName = "ppt_" + timesuffix + ".ppt"; htmFileName = "ppt_" + timesuffix + ".html"; }else{ return null; } File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName); File docInputFile = new File(toFilePath + File.separatorChar + docFileName); if (htmlOutputFile.exists()) htmlOutputFile.delete(); htmlOutputFile.createNewFile(); if (docInputFile.exists()) docInputFile.delete(); docInputFile.createNewFile(); /** * 由fromFileInputStream构建输入文件 */ try { OutputStream os = new FileOutputStream(docInputFile); int bytesRead = 0; byte[] buffer = new byte[1024 * 8]; while ((bytesRead = fromFileInputStream.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fromFileInputStream.close(); } catch (IOException e) { } OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); } catch (ConnectException e) { System.err.println("文件转换出错,请检查OpenOffice服务是否启动。"); } // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docInputFile, htmlOutputFile); connection.disconnect(); // 转换完之后删除word文件 docInputFile.delete(); return htmFileName; } /** * 转换文件成pdf * * @param fromFileInputStream: * @throws IOException */ public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String timesuffix = sdf.format(date); String docFileName = null; String htmFileName = null; if("doc".equals(type)){ docFileName = "doc_" + timesuffix + ".doc"; htmFileName = "doc_" + timesuffix + ".pdf"; }else if("docx".equals(type)){ docFileName = "docx_" + timesuffix + ".docx"; htmFileName = "docx_" + timesuffix + ".pdf"; }else if("xls".equals(type)){ docFileName = "xls_" + timesuffix + ".xls"; htmFileName = "xls_" + timesuffix + ".pdf"; }else if("ppt".equals(type)){ docFileName = "ppt_" + timesuffix + ".ppt"; htmFileName = "ppt_" + timesuffix + ".pdf"; }else{ return null; } File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName); File docInputFile = new File(toFilePath + File.separatorChar + docFileName); if (htmlOutputFile.exists()) htmlOutputFile.delete(); htmlOutputFile.createNewFile(); if (docInputFile.exists()) docInputFile.delete(); docInputFile.createNewFile(); /** * 由fromFileInputStream构建输入文件 */ try { OutputStream os = new FileOutputStream(docInputFile); int bytesRead = 0; byte[] buffer = new byte[1024 * 8]; while ((bytesRead = fromFileInputStream.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fromFileInputStream.close(); } catch (IOException e) { } OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); } catch (ConnectException e) { System.err.println("文件转换出错,请检查OpenOffice服务是否启动。"); } // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docInputFile, htmlOutputFile); connection.disconnect(); // 转换完之后删除word文件 docInputFile.delete(); return htmFileName; } public static void main(String[] args) throws IOException { Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance(); File file = null; FileInputStream fileInputStream = null; /* file = new File("D:/poi-test/exportExcel.xls"); fileInputStream = new FileInputStream(file); // coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls"); coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls");*/ file = new File("F:/test/JavaWeb.doc"); fileInputStream = new FileInputStream(file); coc2HtmlUtil.file2Html(fileInputStream, "F:/test","doc"); // coc2HtmlUtil.file2pdf(fileInputStream, "F:/test","doc"); /* file = new File("D:/poi-test/周报模版.ppt"); fileInputStream = new FileInputStream(file); // coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt"); coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt"); file = new File("D:/poi-test/test.docx"); fileInputStream = new FileInputStream(file); // coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx"); coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx");*/ } } 三、使用poi读取word内容(不成熟): package org.pan.poi.poi; import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import org.apache.commons.io.FileUtils; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.xwpf.extractor.XWPFWordExtractor; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; /** * @author panmingshuai * @description * @Time 2018年4月2日 下午5:46:30 * */ public class WordTest { @SuppressWarnings("resource") public static void main(String[] args) throws Exception { /** * 读取doc文档 * */ FileInputStream in = FileUtils.openInputStream(new File("F:\\物流知识点.doc")); HWPFDocument doc = new HWPFDocument(in); Range range = doc.getRange(); for(int i=0; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有