首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • onClick="javascript:document.all.a.submit();"是什么意思?如果在页面把表格的内容生成一个execl,该怎么做? [已结贴,结贴人:java2000_net]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 14:27:24 楼主
    团体
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-13 14:32:151楼 得分:10
    onClick="javascript:document.all.a.submit();提交名称为a的这个表单
    生成Excel的代码,js需要调用Excel.Application这个ActiveXObject:
    HTML code
    <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <meta http-equiv="pragma" content="no-cache"> <script language="jscript"> function replaceHtml(replacedStr,repStr,endStr){ var replacedStrF = ""; var replacedStrB = ""; var repStrIndex = replacedStr.indexOf(repStr); while(repStrIndex != -1){ replacedStrreplacedStrF = replacedStr.substring(0,repStrIndex); replacedStrreplacedStrB = replacedStr.substring(repStrIndex,replacedStr.length); replacedStrBreplacedStrB = replacedStrB.substring(replacedStrB.indexOf(endStr)+1,replacedStrB.length); replacedStr = replacedStrF + replacedStrB; repStrIndex = replacedStr.indexOf(repStr); } return replacedStr; } //elTalbeOut 这个为导出内容的外层表格,主要是设置border之类的样式,elDiv则是整个导出的html部分 function htmlToExcel(elTableOut,elDiv){ try{ //设置导出前的数据,为导出后返回格式而设置 var elDivelDivStrBak = elDiv.innerHTML; //设置table的border=1,这样到excel中就有表格线 ps:感谢双面提醒 elTableOut.border=1; //过滤elDiv内容 var elDivelDivStr = elDiv.innerHTML; elDivStr = replaceHtml(elDivStr,"<A",">"); elDivStr = replaceHtml(elDivStr,"</A",">"); elDiv.innerHTML=elDivStr; var oRangeRef = document.body.createTextRange(); oRangeRef.moveToElementText( elDiv ); oRangeRef.execCommand("Copy"); //返回格式变换以前的内容 elDiv.innerHTML = elDivStrBak; //内容数据可能很大,所以赋空 elDivStrBak = ""; elDivStr = ""; var oXL = new ActiveXObject("Excel.Application") var oWB = oXL.Workbooks.Add ; var oSheet = oWB.ActiveSheet ; oSheet.Paste(); oSheet.Cells.NumberFormatLocal = "@"; oSheet.Columns("D:D").Select oXL.Selection.ColumnWidth = 20 oXL.Visible = true; oSheet = null; oWB = null; appExcel = null; }catch(e){ alert(e.description) } } </script> <title></title></head> <body leftmargin="0" topmargin="0"> <table width="90%" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td height="40" align="center" valign="middle"><INPUT type="button" value="导出" id="dcExcel" onClick="htmlToExcel(document.getElementById('elTableOut'),document.getElementById('elDiv'));"></td> </tr> </table> <div id="elDiv"> <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#000000" bgcolor="#000000" id="elTableOut"> <tr> <td height="40" align="center" valign="middle" colspan="12" bgcolor="#FFFFFF"><strong>状况汇总表</strong></td> </tr> <tr> <td width="6%" rowspan="2" align="center" bgcolor="#FFFFFF">序号</td> <td width="20%" rowspan="2" align="center" bgcolor="#FFFFFF">a</td> <td width="7%" rowspan="2" align="center" bgcolor="#FFFFFF">b</td> <td width="7%" rowspan="2" align="center" bgcolor="#FFFFFF">c</td> <td width="7%" rowspan="2" align="center" bgcolor="#FFFFFF">d</td> <td width="7%" rowspan="2" align="center" bgcolor="#FFFFFF">e</td> <td width="7%" rowspan="2" align="center" bgcolor="#FFFFFF">f</td> <td width="7%" rowspan="2" align="center" bgcolor="#FFFFFF">g</td> <td height="25" colspan="4" align="center" bgcolor="#FFFFFF">h</td> </tr> <tr> <td width="7%" height="25" align="center" bgcolor="#FFFFFF">i</td> <td width="7%" align="center" bgcolor="#FFFFFF">j</td> <td width="7%" align="center" bgcolor="#FFFFFF">k</td> <td width="7%" align="center" bgcolor="#FFFFFF">l</td> </tr> </table> </div> </body> </html>
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • lmx8757921
    • 等级:
    发表于:2007-11-13 14:40:372楼 得分:10
    strong
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved