js操作html生成excel
程序员文章站
2022-07-06 18:02:00
...
<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){ replacedStrF = replacedStr.substring(0,repStrIndex); replacedStrB = replacedStr.substring(repStrIndex,replacedStr.length); replacedStrB = 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 elDivStrBak = elDiv.innerHTML; //设置table的border=1,这样到excel中就有表格线 ps:感谢双面提醒 elTableOut.border=1; //过滤elDiv内容 var elDivStr = 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>
下一篇: 关于margin和padding的问题
推荐阅读
-
[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)
-
浏览器解析js生成的html出现样式问题的解决方法_javascript技巧
-
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
-
jquery $("nam") 获取不到js生成的html的id ? 来看下代码
-
django框架基于模板 生成 excel(xls) 文件操作示例
-
django框架基于模板 生成 excel(xls) 文件操作示例
-
HTML5的自定义属性data-*详细介绍和JS操作实例
-
js 动态生成html 触发事件传参字符转义的实例
-
JS实现动态生成html table表格的方法分析
-
HTML5的自定义属性data-*详细介绍和JS操作实例