<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( "Copyapp); .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).Activateif. ||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> <br> </tr> <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><br> </div>