Javascript導(dǎo)出Excel的方法
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4<head> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> 6 <title>WEB頁(yè)面導(dǎo)出為EXCEL文檔的方法</title> 7</head> 8<body> 9<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0"> 10 <tr> 11 <td colspan="5" align="center">WEB頁(yè)面導(dǎo)出為EXCEL文檔的方法</td> 12 </tr> 13 <tr> 14 <td>列標(biāo)題1</td> 15 <td>列標(biāo)題2</td> 16 <td>列標(biāo)題3</td> 17 <td>列標(biāo)題4</td> 18 <td>列標(biāo)題5</td> 19 </tr> 20 <tr> 21 <td>aaa</td> 22 <td>bbb</td> 23 <td>ccc</td> 24 <td>ddd</td> 25 <td>eee</td> 26 </tr> 27 <tr> 28 <td>AAA</td> 29 <td>BBB</td> 30 <td>CCC</td> 31 <td>DDD</td> 32 <td>EEE</td> 33 </tr> 34 <tr> 35 <td>FFF</td> 36 <td>GGG</td> 37 <td>HHH</td> 38 <td>III</td> 39 <td>JJJ</td> 40 </tr> 41</table> 42<input type="button" onclick="javascript:method1('tableExcel');" value="第一種方法導(dǎo)入到EXCEL"> 43<input type="button" onclick="javascript:method2('tableExcel');" value="第二種方法導(dǎo)入到EXCEL"> 44<input type="button" onclick="javascript:getXlsFromTbl('tableExcel',null);" value="第三種方法導(dǎo)入到EXCEL"> 45<SCRIPT LANGUAGE="javascript"> 46function method1(tableid) {//整個(gè)表格拷貝到EXCEL中 47 var curTbl = document.getElementById(tableid); 48 var oXL = new ActiveXObject("Excel.Application"); 49 //創(chuàng)建AX對(duì)象excel 50 var oWB = oXL.Workbooks.Add(); 51 //獲取workbook對(duì)象 52 var oSheet = oWB.ActiveSheet; 53 //激活當(dāng)前sheet 54 var sel = document.body.createTextRange(); 55 sel.moveToElementText(curTbl); 56 //把表格中的內(nèi)容移到TextRange中 57 sel.select(); 58 //全選TextRange中內(nèi)容 59 sel.execCommand("Copy"); 60 //復(fù)制TextRange中內(nèi)容 61 oSheet.Paste(); 62 //粘貼到活動(dòng)的EXCEL中 63 oXL.Visible = true; 64 //設(shè)置excel可見屬性 65} 66function method2(tableid) //讀取表格中每個(gè)單元到EXCEL中 67{ 68 var curTbl = document.getElementById(tableid); 69 var oXL = new ActiveXObject("Excel.Application"); 70 //創(chuàng)建AX對(duì)象excel 71 var oWB = oXL.Workbooks.Add(); 72 //獲取workbook對(duì)象 73 var oSheet = oWB.ActiveSheet; 74 //激活當(dāng)前sheet 75 var Lenr = curTbl.rows.length; 76 //取得表格行數(shù) 77 for (i = 0; i < Lenr; i++) 78 { 79 var Lenc = curTbl.rows(i).cells.length; 80 //取得每行的列數(shù) 81 for (j = 0; j < Lenc; j++) 82 { 83 oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText; 84 //賦值 85 } 86 } 87 oXL.Visible = true; 88 //設(shè)置excel可見屬性 89} 90function getXlsFromTbl(inTblId, inWindow) { 91 try { 92 var allStr = ""; 93 var curStr = ""; 94 //alert("getXlsFromTbl"); 95 if (inTblId != null && inTblId != "" && inTblId != "null") { 96 curStr = getTblData(inTblId, inWindow); 97 } 98 if (curStr != null) { 99 allStr += curStr; 100 } 101 else { 102 alert("你要導(dǎo)出的表不存在!"); 103 return; 104 } 105 var fileName = getExcelFileName(); 106 doFileExport(fileName, allStr); 107 } 108 catch(e) { 109 alert("導(dǎo)出發(fā)生異常:" + e.name + "->" + e.description + "!"); 110 } 111} 112function getTblData(inTbl, inWindow) { 113 var rows = 0; 114 //alert("getTblData is " + inWindow); 115 var tblDocument = document; 116 if (!!inWindow && inWindow != "") { 117 if (!document.all(inWindow)) { 118 return null; 119 } 120 else { 121 tblDocument = eval(inWindow).document; 122 } 123 } 124 var curTbl = tblDocument.getElementById(inTbl); 125 var outStr = ""; 126 if (curTbl != null) { 127 for (var j = 0; j < curTbl.rows.length; j++) { 128 //alert("j is " + j); 129 for (var i = 0; i < curTbl.rows[j].cells.length; i++) { 130 //alert("i is " + i); 131 if (i == 0 && rows > 0) { 132 outStr += " \t"; 133 rows -= 1; 134 } 135 outStr += curTbl.rows[j].cells[i].innerText + "\t"; 136 if (curTbl.rows[j].cells[i].colSpan > 1) { 137 for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) { 138 outStr += " \t"; 139 } 140 } 141 if (i == 0) { 142 if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) { 143 rows = curTbl.rows[j].cells[i].rowSpan - 1; 144 } 145 } 146 } 147 outStr += "\r\n"; 148 } 149 } 150 else { 151 outStr = null; 152 alert(inTbl + "不存在!"); 153 } 154 return outStr; 155} 156function getExcelFileName() { 157 var d = new Date(); 158 var curYear = d.getYear(); 159 var curMonth = "" + (d.getMonth() + 1); 160 var curDate = "" + d.getDate(); 161 var curHour = "" + d.getHours(); 162 var curMinute = "" + d.getMinutes(); 163 var curSecond = "" + d.getSeconds(); 164 if (curMonth.length == 1) { 165 curMonth = "0" + curMonth; 166 } 167 if (curDate.length == 1) { 168 curDate = "0" + curDate; 169 } 170 if (curHour.length == 1) { 171 curHour = "0" + curHour; 172 } 173 if (curMinute.length == 1) { 174 curMinute = "0" + curMinute; 175 } 176 if (curSecond.length == 1) { 177 curSecond = "0" + curSecond; 178 } 179 var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_" 180 + curHour + curMinute + curSecond + ".csv"; 181 //alert(fileName); 182 return fileName; 183} 184function doFileExport(inName, inStr) { 185 var xlsWin = null; 186 if (!!document.all("glbHideFrm")) { 187 xlsWin = glbHideFrm; 188 } 189 else { 190 var width = 6; 191 var height = 4; 192 var openPara = "left=" + (window.screen.width / 2 - width / 2) 193 + ",top=" + (window.screen.height / 2 - height / 2) 194 + ",scrollbars=no,width=" + width + ",height=" + height; 195 xlsWin = window.open("", "_blank", openPara); 196 } 197 xlsWin.document.write(inStr); 198 xlsWin.document.close(); 199 xlsWin.document.execCommand('Saveas', true, inName); 200 xlsWin.close(); 201} 202</SCRIPT> 203</body> 204</html> 該文章在 2010/8/18 0:00:09 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |