每一个你不满意的现在,都有一个你不努力的曾经. 网站首页 > js
JS 导出table数据到Excel 前4种方法只支持IE,最后一个支持主流浏览
发布时间:2018-12-10 16:28:21 修改时间:2019-01-03 15:21:24 阅读:6075 评论:0 0
<html> <head></head> <body> html 表格导出 <p></p> <p><span style="font-size:14px;"> //第一种方法 </span></p> <p><span style="font-size:14px;"> function method1(tableid) { </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> var curTbl = document.getElementById(tableid); </span></p> <p><span style="font-size:14px;"> var oXL = new ActiveXObject("Excel.Application"); </span></p> <p><span style="font-size:14px;"> var oWB = oXL.Workbooks.Add(); </span></p> <p><span style="font-size:14px;"> var oSheet = oWB.ActiveSheet; </span></p> <p><span style="font-size:14px;"> var sel = document.body.createTextRange(); </span></p> <p><span style="font-size:14px;"> sel.moveToElementText(curTbl); </span></p> <p><span style="font-size:14px;"> sel.select(); </span></p> <p><span style="font-size:14px;"> sel.execCommand("Copy"); </span></p> <p><span style="font-size:14px;"> oSheet.Paste(); </span></p> <p><span style="font-size:14px;"> oXL.Visible = true; </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> //第二种方法 </span></p> <p><span style="font-size:14px;"> function method2(tableid) </span></p> <p><span style="font-size:14px;"> { </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> var curTbl = document.getElementById(tableid); </span></p> <p><span style="font-size:14px;"> var oXL = new ActiveXObject("Excel.Application"); </span></p> <p><span style="font-size:14px;"> var oWB = oXL.Workbooks.Add(); </span></p> <p><span style="font-size:14px;"> var oSheet = oWB.ActiveSheet; </span></p> <p><span style="font-size:14px;"> var Lenr = curTbl.rows.length; </span></p> <p><span style="font-size:14px;"> for (i = 0; i < Lenr; i++) </span></p> <p><span style="font-size:14px;"> { var Lenc = curTbl.rows(i).cells.length; </span></p> <p><span style="font-size:14px;"> for (j = 0; j < Lenc; j++) </span></p> <p><span style="font-size:14px;"> { </span></p> <p><span style="font-size:14px;"> oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText; </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> oXL.Visible = true; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> //第三种方法 </span></p> <p><span style="font-size:14px;"> function getXlsFromTbl(inTblId, inWindow){ </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> try { </span></p> <p><span style="font-size:14px;"> var allStr = ""; </span></p> <p><span style="font-size:14px;"> var curStr = ""; </span></p> <p><span style="font-size:14px;"> if (inTblId != null && inTblId != "" && inTblId != "null") { </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> curStr = getTblData(inTblId, inWindow); </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> if (curStr != null) { </span></p> <p><span style="font-size:14px;"> allStr += curStr; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> else { </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> alert("你要导出的表不存在"); </span></p> <p><span style="font-size:14px;"> return; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> var fileName = getExcelFileName(); </span></p> <p><span style="font-size:14px;"> doFileExport(fileName, allStr); </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> catch(e) { </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> alert("导出发生异常:" + e.name + "->" + e.description + "!"); </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> function getTblData(inTbl, inWindow) { </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> var rows = 0; </span></p> <p><span style="font-size:14px;"> var tblDocument = document; </span></p> <p><span style="font-size:14px;"> if (!!inWindow && inWindow != "") { </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> if (!document.all(inWindow)) { </span></p> <p><span style="font-size:14px;"> return null; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> else { </span></p> <p><span style="font-size:14px;"> tblDocument = eval(inWindow).document; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> var curTbl = tblDocument.getElementById(inTbl); </span></p> <p><span style="font-size:14px;"> var outStr = ""; </span></p> <p><span style="font-size:14px;"> if (curTbl != null) { </span></p> <p><span style="font-size:14px;"> for (var j = 0; j < curTbl.rows.length; j++) { </span></p> <p><span style="font-size:14px;"> for (var i = 0; i < curTbl.rows[j].cells.length; i++) { </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> if (i == 0 && rows > 0) { </span></p> <p><span style="font-size:14px;"> outStr += " t"; </span></p> <p><span style="font-size:14px;"> rows -= 1; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> outStr += curTbl.rows[j].cells[i].innerText + "t"; </span></p> <p><span style="font-size:14px;"> if (curTbl.rows[j].cells[i].colSpan > 1) { </span></p> <p><span style="font-size:14px;"> for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) { </span></p> <p><span style="font-size:14px;"> outStr += " t"; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> if (i == 0) { </span></p> <p><span style="font-size:14px;"> if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) { </span></p> <p><span style="font-size:14px;"> rows = curTbl.rows[j].cells[i].rowSpan - 1; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> outStr += "rn"; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> else { </span></p> <p><span style="font-size:14px;"> outStr = null; </span></p> <p><span style="font-size:14px;"> alert(inTbl + "不存在 !"); </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> return outStr; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> function getExcelFileName() { </span></p> <p><span style="font-size:14px;"> var d = new Date(); </span></p> <p><span style="font-size:14px;"> var curYear = d.getYear(); </span></p> <p><span style="font-size:14px;"> var curMonth = "" + (d.getMonth() + 1); </span></p> <p><span style="font-size:14px;"> var curDate = "" + d.getDate(); </span></p> <p><span style="font-size:14px;"> var curHour = "" + d.getHours(); </span></p> <p><span style="font-size:14px;"> var curMinute = "" + d.getMinutes(); </span></p> <p><span style="font-size:14px;"> var curSecond = "" + d.getSeconds(); </span></p> <p><span style="font-size:14px;"> if (curMonth.length == 1) { </span></p> <p><span style="font-size:14px;"> curMonth = "0" + curMonth; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> if (curDate.length == 1) { </span></p> <p><span style="font-size:14px;"> curDate = "0" + curDate; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> if (curHour.length == 1) { </span></p> <p><span style="font-size:14px;"> curHour = "0" + curHour; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> if (curMinute.length == 1) { </span></p> <p><span style="font-size:14px;"> curMinute = "0" + curMinute; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> if (curSecond.length == 1) { </span></p> <p><span style="font-size:14px;"> curSecond = "0" + curSecond; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> var fileName = "table" + "_" + curYear + curMonth + curDate + "_" </span></p> <p><span style="font-size:14px;"> + curHour + curMinute + curSecond + ".csv"; </span></p> <p><span style="font-size:14px;"> return fileName; </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> function doFileExport(inName, inStr) { </span></p> <p><span style="font-size:14px;"> var xlsWin = null; </span></p> <p><span style="font-size:14px;"> if (!!document.all("glbHideFrm")) { </span></p> <p><span style="font-size:14px;"> xlsWin = glbHideFrm; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> else { </span></p> <p><span style="font-size:14px;"> var width = 6; </span></p> <p><span style="font-size:14px;"> var height = 4; </span></p> <p><span style="font-size:14px;"> var openPara = "left=" + (window.screen.width / 2 - width / 2) </span></p> <p><span style="font-size:14px;"> + ",top=" + (window.screen.height / 2 - height / 2) </span></p> <p><span style="font-size:14px;"> + ",scrollbars=no,width=" + width + ",height=" + height; </span></p> <p><span style="font-size:14px;"> xlsWin = window.open("", "_blank", openPara); </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> xlsWin.document.write(inStr); </span></p> <p><span style="font-size:14px;"> xlsWin.document.close(); </span></p> <p><span style="font-size:14px;"> xlsWin.document.execCommand('Saveas', true, inName); </span></p> <p><span style="font-size:14px;"> xlsWin.close(); </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> //第四种 </span></p> <p><span style="font-size:14px;"> function method4(tableid){ </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> var curTbl = document.getElementById(tableid); </span></p> <p><span style="font-size:14px;"> var oXL; </span></p> <p><span style="font-size:14px;"> try{ </span></p> <p><span style="font-size:14px;"> oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel </span></p> <p><span style="font-size:14px;"> }catch(e){ </span></p> <p><span style="font-size:14px;"> alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel,"+" 那么请调整IE的安全级别。\n\n具体操作:\n\n"+" 工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用"); </span></p> <p><span style="font-size:14px;"> return false; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> var oWB = oXL.Workbooks.Add(); //获取workbook对象 </span></p> <p><span style="font-size:14px;"> var oSheet = oWB.ActiveSheet;//激活当前sheet </span></p> <p><span style="font-size:14px;"> var sel = document.body.createTextRange(); </span></p> <p><span style="font-size:14px;"> sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中 </span></p> <p><span style="font-size:14px;"> sel.select(); //全选TextRange中内容 </span></p> <p><span style="font-size:14px;"> sel.execCommand("Copy");//复制TextRange中内容 </span></p> <p><span style="font-size:14px;"> oSheet.Paste();//粘贴到活动的EXCEL中 </span></p> <p><span style="font-size:14px;"> oXL.Visible = true; //设置excel可见属性 </span></p> <p><span style="font-size:14px;"> var fname = oXL.Application.GetSaveAsFilename("将table导出到excel.xls" , "Excel Spreadsheets (*.xls), *.xls"); </span></p> <p><span style="font-size:14px;"> oWB.SaveAs(fname); </span></p> <p><span style="font-size:14px;"> oWB.Close(); </span></p> <p><span style="font-size:14px;"> oXL.Quit(); </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> //第五种方法 </span></p> <p><span style="font-size:14px;"> var idTmr; </span></p> <p><span style="font-size:14px;"> function getExplorer() { </span></p> <p><span style="font-size:14px;"> var explorer = window.navigator.userAgent ; </span></p> <p><span style="font-size:14px;"> //ie </span></p> <p><span style="font-size:14px;"> if (explorer.indexOf("MSIE") >= 0) { </span></p> <p><span style="font-size:14px;"> return 'ie'; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> //firefox </span></p> <p><span style="font-size:14px;"> else if (explorer.indexOf("Firefox") >= 0) { </span></p> <p><span style="font-size:14px;"> return 'Firefox'; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> //Chrome </span></p> <p><span style="font-size:14px;"> else if(explorer.indexOf("Chrome") >= 0){ </span></p> <p><span style="font-size:14px;"> return 'Chrome'; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> //Opera </span></p> <p><span style="font-size:14px;"> else if(explorer.indexOf("Opera") >= 0){ </span></p> <p><span style="font-size:14px;"> return 'Opera'; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> //Safari </span></p> <p><span style="font-size:14px;"> else if(explorer.indexOf("Safari") >= 0){ </span></p> <p><span style="font-size:14px;"> return 'Safari'; </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> function method5(tableid) { </span></p> <p><span style="font-size:14px;"> if(getExplorer()=='ie') </span></p> <p><span style="font-size:14px;"> { </span></p> <p><span style="font-size:14px;"> var curTbl = document.getElementById(tableid); </span></p> <p><span style="font-size:14px;"> var oXL = new ActiveXObject("Excel.Application"); </span></p> <p><span style="font-size:14px;"> var oWB = oXL.Workbooks.Add(); </span></p> <p><span style="font-size:14px;"> var xlsheet = oWB.Worksheets(1); </span></p> <p><span style="font-size:14px;"> var sel = document.body.createTextRange(); </span></p> <p><span style="font-size:14px;"> sel.moveToElementText(curTbl); </span></p> <p><span style="font-size:14px;"> sel.select(); </span></p> <p><span style="font-size:14px;"> sel.execCommand("Copy"); </span></p> <p><span style="font-size:14px;"> xlsheet.Paste(); </span></p> <p><span style="font-size:14px;"> oXL.Visible = true; </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> try { </span></p> <p><span style="font-size:14px;"> var fname = oXL.Application.GetSaveAsFilename("Excel.xls" , "Excel Spreadsheets (*.xls), *.xls"); </span></p> <p><span style="font-size:14px;"> } catch (e) { </span></p> <p><span style="font-size:14px;"> print("Nested catch caught " + e); </span></p> <p><span style="font-size:14px;"> } finally { </span></p> <p><span style="font-size:14px;"> oWB.SaveAs(fname); </span></p> <p><span style="font-size:14px;"> oWB.Close(savechanges = false); </span></p> <p><span style="font-size:14px;"> oXL.Quit(); </span></p> <p><span style="font-size:14px;"> oXL = null; </span></p> <p><span style="font-size:14px;"> idTmr = window.setInterval("Cleanup();", 1); </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> else </span></p> <p><span style="font-size:14px;"> { </span></p> <p><span style="font-size:14px;"> tableToExcel(tableid) </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> function Cleanup() { </span></p> <p><span style="font-size:14px;"> window.clearInterval(idTmr); </span></p> <p><span style="font-size:14px;"> CollectGarbage(); </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> var tableToExcel = (function() { </span></p> <p><span style="font-size:14px;"> var uri = 'data:application/vnd.ms-excel;base64,', </span></p> <p><span style="font-size:14px;"> template = ' <meta charset="UTF-8" /></span></p> <table> {table} </table>', <p></p> <p><span style="font-size:14px;"> base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }, </span></p> <p><span style="font-size:14px;"> format = function(s, c) { </span></p> <p><span style="font-size:14px;"> return s.replace(/{(\w+)}/g, </span></p> <p><span style="font-size:14px;"> function(m, p) { return c[p]; }) } </span></p> <p><span style="font-size:14px;"> return function(table, name) { </span></p> <p><span style="font-size:14px;"> if (!table.nodeType) table = document.getElementById(table) </span></p> <p><span style="font-size:14px;"> var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} </span></p> <p><span style="font-size:14px;"> window.location.href = uri + base64(format(template, ctx)) </span></p> <p><span style="font-size:14px;"> } </span></p> <p><span style="font-size:14px;"> })() </span></p> <p><span style="font-size:14px;"><br /></span><br /></p> <p><span style="font-size:14px;"> 导出Excel方法一 导出Excel方法二 导出Excel方法三 导出Excel方法四 导出Excel方法五 html 表格导出道Excel 列标题1 列标题2 类标题3 列标题4 列标题5 aaa bbb ccc ddd eee AAA BBB CCC DDD EEE FFF GGG HHH III JJJ </span></p> </body> </html>
回复列表
关键字词:gt,lt,span,style,font-size,nbsp
上一篇:css常用属性
下一篇:初次使用thinkphp踩过的坑