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 = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String declareno = StringUtils.convertNulltoString(request.getAttribute("declareno"));
String selectId = StringUtils.convertNulltoString(request.getAttribute("selectId"));
%>
<!DOCTYPE html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/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" >
function openDocument(url){
var netOfficeEdit = document.getElementById("riseOffice");
netOfficeEdit.OpenFromURL(url,"","Word.Document");
//写入书签
/* var unit = document.getElementById("danwei").value;
var item = document.getElementById("shixiang").value;
netOfficeEdit.SetbookmarkValue('danwei',unit);
netOfficeEdit.SetbookmarkValue('shixiang',item);
*/
}
function save(){
var netOfficeEdit = document.getElementById("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 = document.getElementById("riseOffice");
netOfficeEdit.FileNew = false;
netOfficeEdit.FileOpen = false;
netOfficeEdit.attachEvent("OnFileCommand",function(cmd,canceled){
if(cmd == 3){
save();
}
document.getElementById("riseOffice").CancelLastCommand = true;
});
});
</script>
</head>
<body style="overflow: hidden;">
<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=