web项目移动端在线预览(excel在线预览)
本项目excel在线预览利用OpenOffice实现
1. 后台实现(前台调用在前面的word转html中)
if(fileVO.getFileName().indexOf(".xls") > -1||fileVO.getFileName().indexOf(".xlsx") > -1||fileVO.getFileName().indexOf(".ppt") > -1||fileVO.getFileName().indexOf(".pptx") > -1){URL url = new URL(Global.imgServer + "?file=" + fileVO.getFilePath() + "&name="+ URLEncoder.encode(fileVO.getFileName(), "utf-8"));HttpURLConnection conn = (HttpURLConnection) url.openConnection();//保存pdf之前,先调用解密方法DataInputStream input = new DataInputStream(conn.getInputStream());String filename = fileVO.getFileName();Boolean flag = request.getHeader("User-Agent").indexOf("like Gecko") > 0;if (request.getHeader("User-Agent").toLowerCase().indexOf("msie") > 0 || flag) {filename = URLEncoder.encode(filename, "UTF-8");// IE浏览器} else {// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,// 这个文件名称用于浏览器的下载框中自动显示的文件名filename = new String(filename.replaceAll(" ", "").getBytes("UTF-8"), "ISO8859-1");}filename=filename.replaceAll("\\+","%20");byte[] buffer = new byte[1024];BufferedInputStream bis = null;File file=new File(LawConfig.officeHtml+fileVO.getFileName());FileOutputStream out=new FileOutputStream(file,true);try {bis = new BufferedInputStream(input);int i = bis.read(buffer);while (i != -1) {out.write(buffer, 0, i);i = bis.read(buffer);}} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) {try {bis.close();} catch (IOException e) {e.printStackTrace();}}if (input != null) {try {input.close();} catch (IOException e) {e.printStackTrace();}}out.close();}System.setProperty("user.dir", "解密接口地址");InteKey mInteKey = new InteKey();System.out.println("开始验证,文件是:"+LawConfig.officeHtml + fileVO.getFileName());int ia = mInteKey.Ia(LawConfig.officeHtml + fileVO.getFileName());System.out.println("验证结果:" + ia);if (ia == 0) { // 加密文件,需要做解密处理System.out.println("开始解密");int da = mInteKey.Da(LawConfig.officeHtml + fileVO.getFileName(), LawConfig.officeHtml + fileVO.getFileName());System.out.println("解密结果:" + da);}InputStream is = new FileInputStream(LawConfig.officeHtml + fileVO.getFileName());//项目路径String path = request.getSession().getServletContext().getRealPath("/").replaceAll("\\\\", "/");String targetPath = path + "/page/mobile_html/";//类型String type = fileVO.getFileName().substring(fileVO.getFileName().lastIndexOf(".")+1);//文件名String fileName = fileVO.getFilePath().substring(0, fileVO.getFilePath().lastIndexOf("."));//这里将方法写到了工具类中String returnPath = file2HtmlUtil.file2Html(is,LawConfig.officeHtml,targetPath,type,fileName);FileInputStream fis = null;OutputStream os = null;fis = new FileInputStream(targetPath+returnPath);os = response.getOutputStream();int count = 0;while ((count = fis.read(buffer)) != -1) {os.write(buffer, 0, count);os.flush();}fis.close();os.close();
工具类
package com.daorigin.law.util;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;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;public class file2HtmlUtil {public static String file2Html(InputStream fromFileInputStream, String sourcePath, String targetPath, String type, String fileName) throws IOException {System.out.println("----------------------------------------------sourcePath:"+sourcePath);System.out.println("----------------------------------------------targetPath:"+targetPath);System.out.println("----------------------------------------------fileName:"+fileName);String docFileName = null;String htmFileName = null;if("xls".equals(type)){docFileName = fileName + ".xls";htmFileName = fileName + ".html";}else if("xlsx".equals(type)){docFileName = fileName + ".xlsx";htmFileName = fileName + ".html";}else if("ppt".equals(type)){docFileName = fileName + ".ppt";htmFileName = fileName + ".html";}else if("pptx".equals(type)){docFileName = fileName + ".pptx";htmFileName = fileName + ".html";}else if("txt".equals(type)){docFileName = fileName + ".odt";htmFileName = fileName + ".html";}else{return null;}File htmlOutputFile = new File( targetPath + File.separatorChar + htmFileName);System.out.println("++++++++++++++++++++++++++++++html路径:"+htmlOutputFile);File docInputFile = new File(sourcePath + File.separatorChar + docFileName);System.out.println("++++++++++++++++++++++++++++++doc路径:"+docInputFile);if (htmlOutputFile.exists())htmlOutputFile.delete();File parentOut = htmlOutputFile.getParentFile(); // 获取父文件if( !parentOut.exists() ) parentOut.mkdirs(); //创建所有父文件夹htmlOutputFile.createNewFile();if (docInputFile.exists())docInputFile.delete();File parentIn = docInputFile.getParentFile(); // 获取父文件if( !parentIn.exists() ) parentIn.mkdirs(); //创建所有父文件夹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) {}//OpenOffice默认端口是8100OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);try {connection.connect();} catch (ConnectException e) {System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");}// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection,new DefaultDocumentFormatRegistry());converter.convert(docInputFile, htmlOutputFile);connection.disconnect();// 转换完之后删除word文件docInputFile.delete();return htmFileName;}
}
我在做excel转换的时候xls没有问题,xlsx转换失败,后来发现是默认定义的excel中没有xlsx格式,这里自己通过集成对应类的方式自定义了xlsx格式
package com.daorigin.law.util;import com.artofsolving.jodconverter.DocumentFamily;
import com.artofsolving.jodconverter.DocumentFormat;public class DefaultDocumentFormatRegistry extends com.artofsolving.jodconverter.DefaultDocumentFormatRegistry {public DefaultDocumentFormatRegistry() {super();//这个是自己加的final DocumentFormat xls = new DocumentFormat("Microsoft Excel", DocumentFamily.SPREADSHEET, "application/vnd.ms-excel", "xlsx");xls.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 97");addDocumentFormat(xls);}}
web项目移动端在线预览(excel在线预览)相关推荐
- 在j2ee的web项目中,执行文件如excel、word导入,文件路径可以是“C:/Users/user/Desktop/abc/abc.xls”这样的路径吗?还是应该是工程的路径:/WEB-INF/
在j2ee的web项目中,执行文件如excel.word导入,文件路径可以是"C:/Users/user/Desktop/abc/abc.xls"这样的路径吗?还是应该是工程的路径 ...
- web项目移动端在线预览(word格式转html)
最近项目中遇到一个需求,需要在手机端实现对pc端上传的附件进行在线预览,整理了一下实现方案,仅供参考 首先是最常见的我word在线预览,这里使用的是com.aspose.words这个jar包(其他格 ...
- java 在线excel_开源Excel在线协同工具
超简单 python让excel飞起来+书 34.9元 包邮 (需用券) 去购买 > 背景 大家好,我是开源电子表格Luckysheet的作者.Luckysheet的github仓库在5个月内获 ...
- office在线预览 word在线预览 .net在线预览 文件在线浏览接口
office在线预览 word在线预览 excel在线预览 文件浏览接口服务 支持移动端浏览,只要能使用浏览器上网都可以使用,不需要安装任何第三方工具. 1.word在线预览 excel在线预览,.n ...
- win7 64位系统web项目导出Excel问题汇总
最近在web项目中做了一个导出Excel功能.在导出的时候报错:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败. 一 ...
- vue移动端实现excel在线预览
上篇博客我提到了ios手机不能实现下载功能,但是可以实现预览,图片预览和pdf预览我已经在前篇博客做了讲解,但是,在工作中大家上传最多的应该是excel的文件,今天我就讲解一下excel移动端的预览实 ...
- WEB端和微信小程序端的文档文件在线预览方法
文件的在线预览方式汇总 文件在线预览功能可以提高用户体验,值得加入. 一般常见的文件有office套装.pdf.txt.md.和音视频. 音视频的预览是单独一块,今天主要说说文档文件的在线预览功能. ...
- web项目使用OpenOffice实现前端在线预览office文档(超详细)
超详细的OpenOffice实现前端在线预览office文档记录 最近搞一个数字化共享平台,是一个java web项目,使用框架ssm,其中项目有一个需要在线预览PDF.excle.ppt.word文 ...
- web浏览器在线预览Excel,PDF,world文档解决方案
众所周知啊,在web浏览器中是无法直接预览Excel.world文档等文件的,PDF有的浏览器是打开预览,有的浏览器是跳转到下载页,行为不一致也是让开发者头疼的事情. 今天给大家提供一个解决方案,实现 ...
- pdfh5.js 基于pdf.js和jQuery,web/h5/移动端PDF预览手势缩放插件。
pdfh5.js 基于pdf.js和jQuery,web/h5/移动端PDF预览手势缩放插件. 注意:本地绝对路径地址不能加载,跨域问题用代理或者服务端解决. svg模式渲染存在缺陷,只能渲染普通pd ...
最新文章
- 5月第三周全球域名解析商Top15:万网DNSPod份额上涨
- 【ASM 翻译系列第二弹:ASM 12C 版本新特性】
- hdu 4309(最大流+枚举状态)
- 牛逼的python代码_牛逼了!Python代码补全利器,提高效率告别996!
- 扬州工业机器人外壳设计排名_工业交换机的外壳设计重要吗?
- mpeg2,mpeg4,h264编码标准的异同
- 【哈利波特】Sherbert Lemon对HP的解读之11
- 机器学习——Day 3 多元线性回归
- 盐城计算机中专学校,盐城市有哪些中专学校?
- [参考]查看ORACLE DB信息的一些SQL
- GraphQL从入门到实战
- layui表格使用复选框批量删除_LayUI表格批量删除方法
- c语言令牌桶原理,令牌桶算法及实现(二)
- node静态服务器tudo
- 100部经典中国电影,你看过几部?
- c语言中的output用法,OUTPUT 子句
- flutter仿微信
- 高德地图放图钉_Google地图中的图钉掉了-如何定位和删除图钉
- 百分百胜率只是个例,我们追求的目标是稳步获利!
- C语言后缀.h文件和.c文件作用和区别