java实现excel、word在线预览
java实现excel、word在线预览
最近项⽬上有⼀个新的需求是实现附件的预览,⽀持word、excel、pdf等附件的在线预览,刚接到任务时⼀脸懵逼,以为直接访问本地的附件地址就可以,结果word、excel直接下载下来了,紧接着去百度各种搜集资料,发现jacob 这个jar包可以实现word、excel转换为pdf,在本地测试了半天这个jar包不⽀持jdk1.7,。⽽后咨询了⼀下同事,同事说可以直接以流的形式输出到页⾯,突发奇想值得尝试,结果成功了,好了不说了,上代码吧。
excel的jsp代码如下
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.uqiansoft.web.util.StringUtils"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";
String declareno = Attribute("declareno"));
String selectId = Attribute("selectId"));
%>
<!DOCTYPE html public"-//w3c//dtd xhtml 1.0 transitional//en""/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>打开⽂档</title>
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/reset.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/tab.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/receive/content.css">
<%@include file="../common.jsp"%>
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/attachement.css" media="screen"/>
<script type="text/javascript">
jquery在线库function openDocument(url){
var netOfficeEdit = ElementById("riseOffice");
netOfficeEdit.OpenFromURL(url,"","Word.Document");
//写⼊书签
/* var unit = ElementById("danwei").value;
var item = ElementById("shixiang").value;
netOfficeEdit.SetbookmarkValue('danwei',unit);
netOfficeEdit.SetbookmarkValue('shixiang',item);
*/
}
function save(){
var netOfficeEdit = ElementById("riseOffice");
if(netOfficeEdit === null || netOfficeEdit ==="undefined"){return false;}
try{
netOfficeEdit.SaveToURL("<%=basePath%>word/insertform?declareno=<%=StringUtils.escapeHTML(declareno, true, true) %>","editfile","","<% =basePath%>word/fawen.doc","form1");
}
catch(err){
alert("err:"+ err.number +":"+ err.description);
}finally{}
}
$(function(){
var bodyheight = document.documentElement.clientHeight||document.body.clientHeight;
$("#riseOffice").height(bodyheight);
if(<%=StringUtils.escapeHTML(selectId,true,true)%>==1){
openDocument("<%=basePath%>word/fawen.doc");
}else if(<%=StringUtils.escapeHTML(selectId,true,true)%>==2){
openDocument("<%=basePath%>word/fawenb.doc");
}else if(<%=StringUtils.escapeHTML(selectId,true,true)%>==3){
openDocument("<%=basePath%>word/fawenN.doc");
}
}
var netOfficeEdit = ElementById("riseOffice");
netOfficeEdit.FileNew =false;
netOfficeEdit.FileOpen =false;
netOfficeEdit.attachEvent("OnFileCommand",function(cmd,canceled){
if(cmd ==3){
save();
}
});
});
</script>
</head>
<body >
<object id="riseOffice" classid="clsid:A39F1330-3322-4a1d-9BF0-0BA2BB90E970" codeBase="<%=basePath%>ocx/OfficeControl.cab#version=5,0,1,8" width="100%">
<param name="BorderStyle" value="0">
<param name="BorderColor" value="14402205">
<param name="TitlebarColor" value="53668">
<param name="TitlebarTextColor" value="0">
<param name="MenubarColor" value="13160660">
<param name="Caption" value="欢迎使⽤!">
<param name="Titlebar" value="0">
<param name="MaxUploadSize" value="10000000">
<param name="CustomMenuCaption" value="辅助选项">
<param name="ProductCaption" value="北京市经济信息中⼼">
<param name="ProductKey" value="3B4C6C82E57D04EF01F5B34A84EFC65FEBC645CA">
<SPAN STYLE="color:red">不能装载⽂档控件。请在检查浏览器的选项中检查浏览器的安全设置。</SPAN>
</object>
</body>
</html>
word的jsp代码如下
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="java.sun/jsp/jstl/core" prefix="c"%>
<%@ page import="com.uqiansoft.web.util.StringUtils"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";
pageContext.setAttribute("ctx", path);
%>
<!DOCTYPE html public"-//w3c//dtd xhtml 1.0 transitional//en""/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="X-UA-Compatible" content="IE=10"/>
<title>打开⽂档模板</title>
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/reset.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/tab.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/receive/content.css">
<%--<%@include file="../common.jsp"%>--%>
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/attachement.css" media="screen"/>
<link rel="stylesheet" type="text/css" href="<%=basePath%>style/table.css">
<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script>
<script type="text/javascript">
var cmnPath ="<%=basePath%>";
var path ="${path}";
$(function(){
var bodyheight = document.documentElement.clientHeight||document.body.clientHeight;
$("#riseOffice").height(bodyheight-30);
var url = cmnPath+"office/openWord?path="+encodeURI(encodeURI(path));
openDocument(url);
var netOfficeEdit = ElementById("riseOffice");
netOfficeEdit.FileNew =false;
netOfficeEdit.FileOpen =false;
netOfficeEdit.FileSaveAs =true;
//netOfficeEdit.FileSave = false;//是否可编辑参数
netOfficeEdit.attachEvent("OnFileCommand",function(cmd,canceled){
});
});
function openDocument(url){
var readonly =false;
var netOfficeEdit = ElementById("riseOffice");
netOfficeEdit.OpenFromURL(url,readonly,"Word.Document");
}
</script>
</head>
<body >
<object id="riseOffice" classid="clsid:A39F1330-3322-4a1d-9BF0-0BA2BB90E970" codeBase="<%=basePath%>ocx/OfficeControl.cab#version=5,0,1,8" width="100%">
<param name="BorderStyle" value="0">
<param name="BorderColor" value="14402205">
<param name="TitlebarColor" value="53668">
<param name="TitlebarTextColor" value="0">
<param name="MenubarColor" value="13160660">
<param name="Caption" value="欢迎使⽤!">
<param name="Titlebar" value="0">
<param name="MaxUploadSize" value="10000000">
<param name="CustomMenuCaption" value="辅助选项">
<param name="ProductCaption" value="北京市经济信息中⼼">
<param name="ProductKey" value="3B4C6C82E57D04EF01F5B34A84EFC65FEBC645CA">
<SPAN STYLE="color:red">不能装载⽂档控件。请在检查浏览器的选项中检查浏览器的安全设置。</SPAN>
</object>
</body>
</html>
pdf的jsp代码如下
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="java.sun/jsp/jstl/core" prefix="c"%>
<%@page import="del.*"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"
+ ServerName()+":"+ ServerPort()
+ path +"/";
request.setAttribute("basePath", basePath);
%>
<!DOCTYPE html public"-//w3c//dtd xhtml 1.0 transitional//en""/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html>
<head>
<base href="<%=basePath%>">
<title>在线预览</title>
<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script>
<script type="text/javascript">
var path ="${path}";
$(function(){
var cmnPath ="<%=basePath%>";
var frame = ElementById("pdfIframe");
path = cmnPath +"office/openPdf?path="+encodeURI(encodeURI(path));
$("#pdfContent").append("<iframe style='height:640px;width:100%;' src='"+path+"'></iframe>");
/* alert(path);
frame.src = path;
});
</script>
<style>
html,body {
height:100%;
}
</style>
</head>
<body>
<span id="pdfContent">
<!--<iframe id="pdfIframe" >
</iframe>-->
</span>
</body>
</html>
图⽚⽂本的jsp如下
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="java.sun/jsp/jstl/core" prefix="c"%>
<%@ page import="com.uqiansoft.web.util.StringUtils"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/"; pageContext.setAttribute("ctx", path);
%>
<!DOCTYPE html public"-//w3c//dtd xhtml 1.0 transitional//en""/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html>
<head>
<base href="<%=basePath%>">
<title>在线预览</title>
<%@include file="../common.jsp"%>
<link rel="stylesheet" type="text/css" href="<%=basePath%>js/magnify/jquery.magnify.min.css" media="screen"/>
<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script>
<%--<script type="text/javascript" src="<%=basePath%>js/jQueryRotate.js"></script>
--%>
<script type="text/javascript" src="<%=basePath%>js/magnify/jquery.magnify.js"></script>
<script type="text/javascript">
var cmnPath ="<%=basePath%>";
var path ="${path}";
var type ="${type}";
$(function(){
var frame = ElementById("mutationImage");
if(type=="image"){
$("#image_div").show();
frame.src = cmnPath+"office/openImage?path="+encodeURI(encodeURI(path));
}else{
$("#txtContent").show();
$("#txtContent").empty();
url = cmnPath+"office/openOtherTxt?path="+encodeURI(encodeURI(path));
$("#txtContent").append('<iframe src="'+url+'"></iframe>');
}
});
function showImage(){
var img_url = cmnPath+"office/openImage?path="+encodeURI(encodeURI(path));
}
</script>
</head>
<body>
<span width="100%;" height="100%;" id="txtContent">
</span>
<div class="image-set" align="center" id="image_div">
<a data-magnify="gallery" id="data_href" href="" onclick="showImage()" data-caption="图⽚预览">
<img width="500;" id="mutationImage" src="" alt="">
</a>
</div>
</body>
</html>
java后端代码如下
package com.ller.word;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论