纯JS将table表格导出到excel的方法
程序员文章站
2022-03-25 13:39:55
html
html
<div > <button type="button" onclick="getxlsfromtbl('tableexcel','mydiv')">ie导出excel方法</button> <button type="button" onclick="method5('tableexcel')">chrome导出excel</button> </div> <div id="mydiv"> <table id="tableexcel" width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td colspan="5" align="center">html 表格导出到excel</td> </tr> <tr> <td>列标题1</td> <td>列标题2</td> <td>类标题3</td> <td>列标题4</td> <td>列标题5</td> </tr> <tr> <td>aaa</td> <td>bbb</td> <td>ccc</td> <td>ddd</td> <td>eee</td> </tr> <tr> <td>aaa</td> <td>bbb</td> <td>ccc</td> <td>ddd</td> <td>eee</td> </tr> <tr> <td>fff</td> <td>ggg</td> <td>hhh</td> <td>iii</td> <td>jjj</td> </tr> </table> </div>
js
<script language="javascript" type="text/javascript"> //ie方法 function getxlsfromtbl(intblid, inwindow){ try { var allstr = ""; var curstr = ""; if (intblid != null && intblid != "" && intblid != "null") { curstr = gettbldata(intblid, inwindow); } if (curstr != null) { allstr += curstr; } else { alert("你要导出的表不存在"); return; } var filename = getexcelfilename(); dofileexport(filename, allstr); } catch(e) { alert("导出发生异常:" + e.name + "->" + e.description + "!"); } } function gettbldata(intbl, inwindow) { var rows = 0; var tbldocument = document; if (!!inwindow && inwindow != "") { if (!document.all(inwindow)) { return null; } else { tbldocument = eval(inwindow).document; } } var curtbl = tbldocument.getelementbyid(intbl); var outstr = ""; if (curtbl != null) { for (var j = 0; j < curtbl.rows.length; j++) { for (var i = 0; i < curtbl.rows[j].cells.length; i++) { if (i == 0 && rows > 0) { outstr += " t"; rows -= 1; } outstr += curtbl.rows[j].cells[i].innertext + "t"; if (curtbl.rows[j].cells[i].colspan > 1) { for (var k = 0; k < curtbl.rows[j].cells[i].colspan - 1; k++) { outstr += " t"; } } if (i == 0) { if (rows == 0 && curtbl.rows[j].cells[i].rowspan > 1) { rows = curtbl.rows[j].cells[i].rowspan - 1; } } } outstr += "rn"; } } else { outstr = null; alert(intbl + "不存在 !"); } return outstr; } function getexcelfilename() { var d = new date(); var curyear = d.getyear(); var curmonth = "" + (d.getmonth() + 1); var curdate = "" + d.getdate(); var curhour = "" + d.gethours(); var curminute = "" + d.getminutes(); var cursecond = "" + d.getseconds(); if (curmonth.length == 1) { curmonth = "0" + curmonth; } if (curdate.length == 1) { curdate = "0" + curdate; } if (curhour.length == 1) { curhour = "0" + curhour; } if (curminute.length == 1) { curminute = "0" + curminute; } if (cursecond.length == 1) { cursecond = "0" + cursecond; } var filename = "table" + "_" + curyear + curmonth + curdate + "_" + curhour + curminute + cursecond + ".csv"; return filename; } function dofileexport(inname, instr) { var xlswin = null; if (!!document.all("glbhidefrm")) { xlswin = glbhidefrm; } else { var width = 6; var height = 4; var openpara = "left=" + (window.screen.width / 2 - width / 2) + ",top=" + (window.screen.height / 2 - height / 2) + ",scrollbars=no,width=" + width + ",height=" + height; xlswin = window.open("", "_blank", openpara); } xlswin.document.write(instr); xlswin.document.close(); xlswin.document.execcommand('saveas', true, inname); xlswin.close(); } //chrome方法 var idtmr; function getexplorer() { var explorer = window.navigator.useragent ; //ie if (explorer.indexof("msie") >= 0) { return 'ie'; } //firefox else if (explorer.indexof("firefox") >= 0) { return 'firefox'; } //chrome else if(explorer.indexof("chrome") >= 0){ return 'chrome'; } //opera else if(explorer.indexof("opera") >= 0){ return 'opera'; } //safari else if(explorer.indexof("safari") >= 0){ return 'safari'; } } function method5(tableid) { if(getexplorer()=='ie') { var curtbl = document.getelementbyid(tableid); var oxl = new activexobject("excel.application"); var owb = oxl.workbooks.add(); var xlsheet = owb.worksheets(1); var sel = document.body.createtextrange(); sel.movetoelementtext(curtbl); sel.select(); sel.execcommand("copy"); xlsheet.paste(); oxl.visible = true; try { var fname = oxl.application.getsaveasfilename("excel.xls", "excel spreadsheets (*.xls), *.xls"); } catch (e) { print("nested catch caught " + e); } finally { owb.saveas(fname); owb.close(savechanges = false); oxl.quit(); oxl = null; idtmr = window.setinterval("cleanup();", 1); } } else { tabletoexcel(tableid) } } function cleanup() { window.clearinterval(idtmr); collectgarbage(); } var tabletoexcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,', template = '<html><head><meta charset="utf-8"></head><body><table>{table}</table></body></html>', base64 = function(s) { return window.btoa(unescape(encodeuricomponent(s))) }, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) } return function(table, name) { if (!table.nodetype) table = document.getelementbyid(table) var ctx = {worksheet: name || 'worksheet', table: table.innerhtml} window.location.href = uri + base64(format(template, ctx)) } })() </script>
到此这篇关于纯js将table表格导出到excel的方法的文章就介绍到这了,更多相关js table表格导出excel内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
下一篇: 美女变巫婆-不可思议的算术题