原生js导出Excel表格,兼容ie

html页面table导出Excel表格

写在前:找的别人的轮子自己修改,原文具体出处无法考证。
问题1:原文导出时,无法修改文件名。本人修改后已解决问题
问题2:js代码不能直接放置于html页面当中,会导致初始化错误,建议以script标签引入。
问题3:如果在非ie情况下,导出的Excel默认是没有网格线的,以wps为例,在【视图】菜单,需要手动开启一下网格线。其余操作正常。
其它:ie浏览器导出方式,默认是打开Excel相关软件,手动保存。
目前测试在ie5环境也能导出。

测试demo如下:

html

DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>table 导出excel表格title><script type="text/javascript" src="excel.js">script>head><body><span id="filename">测试文件名span><div><button type="button" onclick="CreateExcel('tableid')">导出Excelbutton>div><div><table id="tableid" width="100%" border="1" cellspacing="0" cellpadding="4" align="center"style="border-collapse: collapse;"><tr><td>测试标题1td><td>测试标题2td><td>测试标题3td><td>测试标题4td><td>测试标题5td>tr><tr><td>测试标题1td><td>测试标题2td><td>测试标题3td><td>测试标题4td><td>测试标题5td>tr><tr><td>测试标题1td><td>测试标题2td><td>测试标题3td><td>测试标题4td><td>测试标题5td>tr><tr><td>测试标题1td><td>测试标题2td><td>测试标题3td><td>测试标题4td><td>测试标题5td>tr>table>div>body>
html>

excel.js

//HTML导出Excel文件(兼容IE及所有浏览器)
function CreateExcel(tableid) {//设置导出时Excel文件的名称var filename = document.getElementById("filename").innerText//判断导出浏览器if (getExplorer() == 'ie' || getExplorer() == undefined) {HtmlExportToExcelForIE(tableid, filename);} else {HtmlExportToExcelForEntire(tableid, filename)}
}
//IE浏览器导出Excel
function HtmlExportToExcelForIE(tableid, filename) {try {var curTbl = document.getElementById(tableid);var oXL;try {oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel} catch (e) {alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel," + "那么请调整IE的安全级别。\n\n具体操作:\n\n" +"工具 → Internet选项 → 安全 → 自定义级别 → 对未标记为可安全执行脚本的ActiveX初始化并脚本运行 → 启用");return false;}var oWB = oXL.Workbooks.Add(); //获取workbook对象var oSheet = oWB.ActiveSheet; //激活当前sheetvar sel = document.body.createTextRange();sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中try {sel.select(); //全选TextRange中内容} catch (e1) {e1.description}sel.execCommand("Copy"); //复制TextRange中内容oSheet.Paste(); //粘贴到活动的EXCEL中oXL.Visible = true; //设置excel可见属性var fname = oXL.Application.GetSaveAsFilename(filename + ".xlsx","Excel Spreadsheets (*.xlsx), *.xlsx"); //可以修改格式为xlsoWB.SaveAs(fname);oWB.Close();oXL.Quit();} catch (e) {alert(e.description);}
}//非IE浏览器导出Excel
var HtmlExportToExcelForEntire = (function() {var uri = 'data:application/vnd.ms-excel;base64,',template ='' +'' +'' +'' +'' +'{table}
'
+'' +'',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);}//获取table的html以及内容var get_table_html = {worksheet: name || 'Worksheet',table: table.innerHTML}//获取base64的数据var get_url = uri + base64(format(template, get_table_html));//模拟a标签,点击时下载var a_tag = document.createElement('a');a_tag.href = get_url;a_tag.target = '_blank'a_tag.download = name + '.xlsx'; //如果去除'.xlsx'后缀,默认是xls格式a_tag.style.display = 'none'document.body.append(a_tag)a_tag.click();} })()//判定浏览器方法 function getExplorer() {var explorer = window.navigator.userAgent;if (explorer.indexOf("MSIE") >= 0) {return 'ie';} else if (explorer.indexOf("Firefox") >= 0) {return 'Firefox';} else if (explorer.indexOf("Chrome") >= 0) {return 'Chrome';} else if (explorer.indexOf("Opera") >= 0) {return 'Opera';} else if (explorer.indexOf("Safari") >= 0) {return 'Safari';} }


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部