<div class="codetitle"> <span><a style="CURSOR: pointer" data="36288" class="copybut" id="copybut36288" onclick="doCopy('code36288')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code36288"> <br> <br> <br> <br><title>导出Excel</title> <br><script type="text/javascript"> <BR>var idTmr = ""; <BR>function killExcelProcess(appExcel_){ <BR>appExcel_.Quit(); <BR>appExcel_ = null; <BR>idTmr = window.setInterval("Cleanup();",1); <BR>} <br><br>//导出到excel,参数tableid为JSP页面需要导出的table的id,需要更改IE的安全设置,ActiveX都打开,如果还是不能使用,再服务端运行“regsvr32 scrrun.dll” <BR>function exportToExcel(tableid,notitleandsearch){ <BR>try { <BR>clipboardData.setData('Text',''); <BR>var appExcel = new ActiveXObject("Excel.Application"); <br><br>killExcelProcess(appExcel); <BR>appExcel.workbooks.add; <br><br>//标题 <BR>if (notitleandsearch==null||notitleandsearch==false){ <BR>var elTable = document.getElementById('div_title'); <BR>var oRangeRef = document.body.createTextRange(); <BR>oRangeRef.moveToElementText(elTable); <BR>oRangeRef.execCommand( "Copy" ); <BR>appExcel.ActiveSheet.Cells(1,3).select(); <BR>appExcel.ActiveSheet.Paste(); <br><br>clipboardData.setData('Text',''); <BR>appExcel.ActiveSheet.Cells(2,1).select(); <BR>appExcel.ActiveSheet.Paste(); <BR>} <br><br>var elTable1 = document.getElementById(tableid); <BR>var oRangeRef1 = document.body.createTextRange(); <BR>oRangeRef1.moveToElementText(elTable1); <BR>oRangeRef1.execCommand( "Copy" ); <br><br>appExcel.WorkSheets(1).Activate; <BR>if (notitleandsearch==null||notitleandsearch==false){ <BR>appExcel.ActiveSheet.Cells(3,1).select(); <BR>}else{ <BR>appExcel.ActiveSheet.Cells(1,1).select(); <BR>} <BR>appExcel.WorkSheets(1).Activate; <BR>appExcel.ActiveSheet.Paste(); <BR>appExcel.Visible = true; <br><br>} catch(e) { <BR>alert("请确认IE安全设置,ActiveX都启用!"); <BR>return false; <BR>} <BR>clipboardData.setData('text',''); <BR>} <BR></script> <br> <br><br> <br><button onclick="javascript:exportToExcel('testList','');">导出</button><br> <br><br><div id="div_title"> <br><font color="black" size="4"><strong>导出报表</strong></font> <br> </div> <br><table id="testList" bordercolor="#000000" border="1"> <br><tr> <br><td>序列</td> <br><td>姓名</td> <br><td>数量</td> <br> </tr> <br><tr> <br><td>1</td> <br><td>张三1</td> <br><td>2</td> <br> </tr> <br><tr> <br><td>2</td> <br><td>张三2</td> <br><td>2</td> <br> </tr> <br><tr> <br><td>3</td> <br><td>张三3</td> <br><td>2</td> <br> </tr> <br><tr> <br><td>4</td> <br><td>张三4</td> <br><td>2</td> <br> </tr> <br><tr> <br><td>5</td> <br><td>张三5</td> <br><td>2</td> <br> </tr> <br><tr> <br><td colspan="2">总计:</td> <br><td>10</td> <br> </tr> <br><br> </table> <br> <br> <br> </div>