在线等,Java中如何实现在jsp页面在线预览文档
RT
文档上传做好了
在数据库中存的是上传文档的路径
现在项目需求是要点击文档名称在jsp页面中打开该文档,类似百度文库那种的预览,
文档格式目前有 PPT WORD PDF三种
在网上找了
很多使用poi 来解析的
但目前只做到了解析文字,且解析出来后,打开很慢,这样肯定不行
格式和图片表格 都解析不出来
很纠结
还有几十 用flexpaper 来转换成swf文件在预览显示
但是还是没能搞定,有些软件还不是免费或者开源的,不能随便用商业版的软件
越加纠结
谁有做好的Demo 啊
在线等啊
大侠
[解决办法]
没那么简单,最好是后台批处理把所上传文档都统一转为 PDF 或某种格式,前台预览时直接打开该PDF。
转换过程建议用工具,不要限于Java实现;自己用POI之类的进行解析实现效率太低了。
[解决办法]
得用到第三方插件,否则没办法做.我们公司都是用第三方插件,比如excel在IE中显示.估计你在搜索中没有例子,我都搜了个遍了
给你一个excel的例子,只能在IE中用,其他浏览器不支持.
重要的是这句
<OBJECT id="Spreadsheet1" classid="clsid:0002E559-0000-0000-C000-000000000046" name="Spreadsheet1" style="width:100%;height:421px" >
IPA : OWCVBA11.CHM 自己去下载玩玩吧
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript">
function document.onkeydown(){
if(event.keyCode == 116){
event.keyCode = 0;
event.returnValue = false;
return;
}
}
if(window.Event){
document.captureEvents(Event.MOUSEUP);
}
function nocontextmenu(){
event.cancelBubble = true
event.returnValue = false;
return false;
}
function norightclick(e) {
if (window.Event){
if (e.which == 2
[解决办法]
e.which == 3)
return false;
}else if(event.button == 2
[解决办法]
event.button == 3) {
event.cancelBubble = true
event.returnValue = false;
return false;
}
}
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; // for all others
function deleteRecord(){
var Spreadsheet1 = document.all("Spreadsheet1");
//var rowNum = Spreadsheet1.ActiveCell.Row;
//Spreadsheet1.Cells.Rows(rowNum).Delete();
var rowNums = Spreadsheet1.Selection.Rows;
rowNums.Delete();
}
function undo(){
var Spreadsheet1 = document.all("Spreadsheet1");
if(Spreadsheet1.CanUndo){
Spreadsheet1.Undo();
}else{
alert("无法执行撤销操作!");
}
}
function clearAll(){
var Spreadsheet1 = document.all("Spreadsheet1");
Spreadsheet1.Range("A2:DD500").Cells.Clear();
}
function Spreadsheet1_BeforeContextMenu(x, y, Menu, Cancel){
var cmContextMenu = new Array();
cmContextMenu[0] = ["撤销操作(&U)", "undo"];
cmContextMenu[1] = null;
cmContextMenu[2] = ["删除行记录(&D)", "deleteRecord"];
cmContextMenu[3] = ["清空所有记录(&A)", "clearAll"];
Menu.Value = cmContextMenu;
//Cancel.Value = true;
}
function Spreadsheet1_CommandExecute(Command, Succeeded){
if(Command == 'undo') undo();
if(Command == 'deleteRecord') deleteRecord();
if(Command == 'clearAll') clearAll();
}
function Spreadsheet1_BeforeKeyDown(KeyCode, Shift, Cancel){
//屏蔽剪切复制
if((Shift == 2) && (KeyCode == 88
[解决办法]
KeyCode == 67)) Cancel.Value = true;
}
function onload() {
var Spreadsheet1 = document.all("Spreadsheet1");
Spreadsheet1.EnableUndo = false;
//设置显示工具栏
Spreadsheet1.DisplayToolbar = false;
//设置调整大小
Spreadsheet1.ActiveWindow.EnableResize = false;
//alert(arr.length);
//显示“命令和选项”窗口
Spreadsheet1.AllowPropertyToolbox = false;
//设置单个sheet属性
Spreadsheet1.DisplayWorkbookTabs = true;
var aa = eval("Spreadsheet1"+"_BeforeContextMenu");
//调用事件
Spreadsheet1.attachEvent("BeforeContextMenu",aa);
Spreadsheet1.attachEvent("CommandExecute",Spreadsheet1_CommandExecute);
Spreadsheet1.attachEvent("BeforeKeyDown",Spreadsheet1_BeforeKeyDown);
Spreadsheet1.ActiveSheet.Cells.Clear();
Spreadsheet1.ActiveSheet.Cells(2, 1).Value = "Car";
Spreadsheet1.ActiveSheet.Cells(3, 1).Value = "Sport-Utility";
Spreadsheet1.ActiveSheet.Cells(4, 1).Value = "Truck";
Spreadsheet1.ActiveSheet.Cells(5, 1).Value = "Minivan";
Spreadsheet1.ActiveSheet.Cells(1, 2).Value = "1998";
Spreadsheet1.ActiveSheet.Cells(2, 2).Value = 0.2;
Spreadsheet1.ActiveSheet.Cells(3, 2).Value = 0.06;
Spreadsheet1.ActiveSheet.Cells(4, 2).Value = 0.17;
Spreadsheet1.ActiveSheet.Cells(5, 2).Value = 0.13;
Spreadsheet1.ActiveSheet.Cells(1, 3).Value = "1999";
Spreadsheet1.ActiveSheet.Cells(2, 3).Value = 0.38;
Spreadsheet1.ActiveSheet.Cells(3, 3).Value = 0.82;
Spreadsheet1.ActiveSheet.Cells(4, 3).Value = 0.28;
Spreadsheet1.ActiveSheet.Cells(5, 3).Value = 0.62;
Spreadsheet1.ActiveSheet.Cells(1, 4).Value = "2000";
Spreadsheet1.ActiveSheet.Cells(2, 4).Value = 0.42;
Spreadsheet1.ActiveSheet.Cells(3, 4).Value = 0.12;
Spreadsheet1.ActiveSheet.Cells(4, 4).Value = 0.55;
Spreadsheet1.ActiveSheet.Cells(5, 4).Value = 0.25;
//设置保护区域
var shtCurrent = Spreadsheet1.ActiveSheet;
var rngUsed = shtCurrent.UsedRange;
var rngCurRegion = Spreadsheet1.Range("a2").CurrentRegion;
alert(rngUsed.Rows.Count);
//alert(rngUsed.Columns.Count);
//Spreadsheet1.Cells.Rows(2).Delete();
//shtCurrent.range(shtCurrent.Cells(5, 1),shtCurrent.Cells(5, 4)).Locked = false;
//shtCurrent.Protection.Enabled = true;
Spreadsheet1.EnableUndo = true;
//alert(Spreadsheet1.XMLData);
//alert(Spreadsheet1.ActiveWorkbook.XmlMaps(1).MapData);
}
</script>
</head>
<body onload="return onload()" >
<form id="form1" runat="server">
<div>
</div>
</form>
<OBJECT id="Spreadsheet1" classid="clsid:0002E559-0000-0000-C000-000000000046" name="Spreadsheet1" style="width:100%;height:421px" >
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td bgcolor="#336699" height="25" width="10%"> </td>
<td bgcolor="#666666" width="85%">
<font face="宋体" color="white" size="4">
<b> 缺少 Microsoft Office Web Components</b>
</font>
</td>
</tr>
<tr>
<td bgcolor="#cccccc" width="15"> </td><td bgcolor="#cccccc" width="500px"><br>
<font face="宋体" size="2">此网页要求 Microsoft Office Web Components。
<p align="center">
<a href="C:/IUware Online/Microsoft Office Professional Enterprise Edition 2003/files/owc11/setup.exe"> 单击此处安装 Microsoft Office Web Components。
</a>.
</p>
</font>
<p>
<font face="宋体" size="2"> 此网页同时要求 Microsoft Internet Explorer 5.01 或更高版本。</p>
<p align="center"/><a href="http://www.microsoft.com/windows/ie/default.htm"> 单击此处安装最新的 Internet Explorer</a>.
</font><br>
</td>
</tr>
</table>
</OBJECT>
</body>
</html>
window.onload = function() {
document.getElementById("divwrapped").innerHTML = "<iframe width='100%' height='100%' src='ajax_pageload.action?path=" + encodeURIComponent(filepath) + "' />"; };
response.setHeader("Content-type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
// 设置下载头信息
try {
TransmitFile.downLoad(response, strUrl);
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
out.flush();
out.close();
/**
* 下载文件
* @param config
* @param response
* @param downLoadFileName
*/
public static void downLoad(HttpServletResponse response,String filePath){
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
String filename=filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));
bis =new BufferedInputStream(new FileInputStream(filePath));
bos=new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesread;
while(-1 != (bytesread = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesread);
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}