轉自: http://www.iteye.com/topic/178043
今天無意找到了很久前的js匯出excel功能,用了一下感覺不錯,在網上看很多人做js匯出excel,可是對錶格線的匯出excel總是頭疼,我這裡有一個簡單的匯出js,大家可以看看。
這隻是一個簡單demo,首先靜態頁面
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>綜合資訊管理系統 行政管理 安全檢查 歸檔資訊 列表</title><link href="../../resources/css/stlye.css" rel="stylesheet" type="text/css" /><style type="text/css">body {margin-top: 10px;margin-bottom: 5px;margin-left: 5px;margin-right: 5px;}</style><body><table width="90%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="30" align="left" class="zibold13">當前位置:行政管理 >> 安全檢查 >> <a href="aqGuidang.html">歸檔資訊</a> >> 列表</td> </tr></table><table width="90%" align="center" border="0" cellpadding="0" cellspacing="0" class="neikuangtop"> <tr> <td width="7%" height="30" align="right" class="left_line">時間:</td><td width="15%" align="left"><input name="textfield" type="text" value="2006-09-18" size="15" height="16"></td><td width="3%" align="right">至</td> <td width="55%" align="left"><input name="textfield2" type="text" value="2007-09-20" size="15" height="16" /></td> <td width="10%" align="right" style="padding-right:10px;"><input type="button" name="Submit" value="查詢" onclick="javascript:location.href='#';" /></td> <td width="10%" align="left" style="padding-left:10px;"><input type="button" name="Submit3" value="查詢統計" onclick="javascript:location.href='aqJiancha_Search.html';" /></td> </tr></table><table id="table1" width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="waikuang"> <tr> <td width="6%" height="27" align="center" class="neikuangbold">序號</td><td width="10%" align="center" class="neikuangbold">時間</td> <td width="20%" align="center" class="neikuangbold">單位</td> <td width="18%" align="center" class="neikuangbold">項目</td> <td width="10%" align="center" class="neikuangbold">結果等級</td> <td width="10%" align="center" class="neikuangbold">檢查機關</td> <td width="8%" align="center" class="neikuangbold">記錄人</td> <td width="12%" align="center" class="neikuangbold">記錄時間</td> <td width="6%" align="center" class="neikuangbold">查看</td> </tr> <tr> <td height="21" align="center" class="neikuang">1</td><td align="center" class="neikuang">1980-02-02</td> <td align="center" class="neikuang">武警總隊</td> <td align="center" class="neikuang">安全用電檢查</td> <td align="center" class="neikuang">2222222</td> <td align="center" class="neikuang">總部</td> <td align="center" class="neikuang">主席</td> <td align="center" class="neikuang">1980-02-02</td> <td align="center" class="neikuang"><a href="aqChakanShow.html">查看</a></td> </tr> <tr> <td height="21" align="center" class="neikuang">2</td><td align="center" class="neikuang">1980-02-02</td> <td align="center" class="neikuang">武警總隊</td> <td align="center" class="neikuang">安全用電檢查</td> <td align="center" class="neikuang">2222222</td> <td align="center" class="neikuang">總部</td> <td align="center" class="neikuang">總理</td> <td align="center" class="neikuang">1980-02-02</td> <td align="center" class="neikuang"><a href="aqChakanShow.html">查看</a></td> </tr> </table><table width="90%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="25" align="right">首頁 | 上一頁 | 下一頁 | 尾頁</td> </tr></table><table width="90%" align="center" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="50%" height="30" align="right" valign="bottom" style="padding-right:10px;"><input type="submit" name="Submit2" onclick="preview()" value="列印當前頁" class="waikuang" ></td><td width="50%" align="left" valign="bottom" style="padding-left:10px;"><input type="submit" name="Submit22" value="列印所有頁"></td> </tr></table></body></html>
下面,調用js
<script language=javascript>function preview() { window.clipboardData.setData("Text",document.all('table1').outerHTML);try{var ExApp = new ActiveXObject("Excel.Application")var ExWBk = ExApp.workbooks.add()var ExWSh = ExWBk.worksheets(1)ExApp.DisplayAlerts = falseExApp.visible = true} catch(e){alert("您的電腦沒有安裝Microsoft Excel軟體!")return false} ExWBk.worksheets(1).Paste; }</script>
很簡單,有興趣的朋友可以試試。
這個js是根據table的id進行匯出的,這樣有個缺點:會將最後一列的“查看”進行匯出excel輸出。解決方案,在此頁面再輸出一個隱藏的table並給他標示,這個隱藏table只有資料,這樣點擊匯出,只匯出資料。方法繁重,希望有別的解決方案,如果有請指教。