注意:IE浏览器需要以下设置:

打开IE,在常用工具栏中选择“工具”--->Internet选项---->选择"安全"标签页--->选择"自定义级别"--->弹出的窗口中找到ActiveX控件和插件--->
将"ActiveX控件自动提示"
"对没有标记安全的ActiveX控件进行初始化和脚本运行"
"下载未签名的ActiveX控件"
三项 选择启用 即可!

HtmlExportToExcel.js 代码

 /*
* HTML导出Excel文件(兼容IE及所有浏览器)
* @param {any} tableid table父元素ID
* @param {any} filename 文件名称
*/
function HtmlExportToExcel(tableid, filename) {
if (getExplorer() === 'ie' || getExplorer() === undefined) {
HtmlExportToExcelForIE(tableid, filename);
}
else {
HtmlExportToExcelForEntire(tableid, filename);
}
} //IE浏览器导出Excel
function HtmlExportToExcelForIE(tableId, filename) {
try {
var oXL = new ActiveXObject("excel.Application");
//oXL.Visible = true;
//oXL.ScreenUpdating = false;
} catch (e1) {
try {
oXL = new ActiveXObject("et.Application");
} catch (e2) {
alert(e2.description + "\n\n\n要使用EXCEL对象,您必须安装Excel电子表格软件\n或者,需要安装Kingsoft ET软件\n\n同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。");
return;
}
}
//创建AX对象excel
var oWB = oXL.Workbooks.Add();
//获取workbook对象
var xlsheet = oWB.Worksheets(); var elTable = document.getElementById(tableId); //替换掉表格td中隐藏的html元素
var tableHtml = ReplaceHtml(elTable.innerHTML); var newTable = document.getElementById("newData");
//console.log();
newTable.innerHTML = tableHtml; //激活当前sheet
var sel = document.body.createTextRange();
sel.moveToElementText(newTable);
//把表格中的内容移到TextRange中
sel.select;
//全选TextRange中内容
sel.execCommand("Copy");
//复制TextRange中内容
xlsheet.Paste();
//粘贴到活动的EXCEL中
oXL.Visible = true;
//设置excel可见属性 newTable.innerHTML = ""; try {
//设置 sheet 名称
xlsheet.Name = filename;
var fname = oXL.Application.GetSaveAsFilename(filename + ".xls", "Excel Spreadsheets (*.xls), *.xls");
} catch (e) {
print("Nested catch caught " + e);
} finally {
oWB.SaveAs(fname);
oWB.Close();
//xls.visible = false;
oXL.ScreenUpdating = true;
oXL.Quit();
}
} //非IE浏览器导出Excel
var HtmlExportToExcelForEntire = (function () {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->' +
/**********这部分是加载表格的样式 没有样式可以省略 start**********/
'<style type="text/css">' +
'.tablefrom {width: 100%;border-collapse: collapse;}' +
'.tablefrom, .tablefrom td, .tablefrom th {text-align: center;font: 12px Arial, Helvetica, sans-serif;border: 1px solid #fff;}' +
'.tablefrom th{background:#328aa4;color:#fff;}' +
'.tablefrom td{background:#e5f1f4;}' +
'.tablefrom .BlueBgColor td {color: #fff;background-color: #0070c0;}' +
'.tablefrom .LightBlueBgColor td {color: #000000;background-color: #bdd7ee;}' +
'.tablefrom tr .BlueBgColorTd { color: #fff;background-color: #0070c0;}' +
'.tablefrom tr .LightBlueBgColorTd {color: #000000;background-color: #bdd7ee;}' +
'</style>'
/**********这部分是加载表格的样式 没有样式可以省略 end**********/
+ '</head><body>{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); }
//替换掉表格td中隐藏的html元素
var strHTML = ReplaceHtml(table.innerHTML);
var ctx = { worksheet: name || 'Worksheet', table: strHTML }; document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = name + ".xls";
document.getElementById("dlink").click();
};
})(); //获取当前使用浏览器
function getExplorer() {
var explorer = window.navigator.userAgent;
//ie
if (explorer.indexOf("MSIE") >= ) {
return 'ie';
}
//firefox
else if (explorer.indexOf("Firefox") >= ) {
return 'Firefox';
}
//Chrome
else if (explorer.indexOf("Chrome") >= ) {
return 'Chrome';
}
//Opera
else if (explorer.indexOf("Opera") >= ) {
return 'Opera';
}
//Safari
else if (explorer.indexOf("Safari") >= ) {
return 'Safari';
}
} //将隐藏的HTML元素替换掉
function ReplaceHtml(tableHtml) {
var radioValue = $('input[name="bedStatus"]:checked ').val();
if (radioValue === 'yuan') {
tableHtml = tableHtml.replace(/<span class="span_wanyuan" [^<>]*?>(.*?)<\/span>/gi, "");
}
else if (radioValue === 'wanyuan') {
tableHtml = tableHtml.replace(/<span class="span_yuan" [^<>]*?>(.*?)<\/span>/gi, "");
}
return tableHtml;
}

另外,我的功能需要替换掉td中隐藏的html元素,所以需要将新的table复制到另一个隐藏的div中。

页面html代码:

 <div id="divData">
<table class="tablefrom" id="tbData" style="width: 100%; border-collapse: collapse;" border="" cellspacing="">
<tbody>
<tr>
<th rowspan="" colspan="" scope="col">序号</th>
<th style="min-width: 70px;" rowspan="" scope="col">项目</th>
<th style="min-width: 50px;" rowspan="" scope="col">维度</th>
<th style="min-width: 50px;" rowspan="" scope="col">本月资金</th>
<th rowspan="" colspan="" scope="col">某部门</th>
<th colspan="" scope="col">支付</th>
</tr>
<tr>
<th style="min-width: 50px;" scope="col">执行</th>
<th style="min-width: 50px;" scope="col">余额</th>
</tr>
<tr>
<td style="color: rgb(0, 0, 0); font-weight: bold; background-color: rgb(189, 215, 238);" rowspan="" scope="col">经营费用类</td>
<td style="min-width: 50px;" rowspan="" scope="col"></td>
<td rowspan="" scope="col">销售退款</td>
<td scope="col">实际</td>
<td style="color: rgb(0, 0, 0); background-color: rgb(189, 215, 238);" scope="col"><span class="span_yuan"></span><span class="span_wanyuan" style="display: none;"></span></td>
<td scope="col"><span class="span_yuan"></span><span class="span_wanyuan" style="display: none;"></span></td>
<td scope="col"><span class="span_yuan"></span><span class="span_wanyuan" style="display: none;"></span></td>
<td scope="col"><span class="span_yuan"></span><span class="span_wanyuan" style="display: none;"></span></td>
</tr>
<tr>
<td scope="col">计划</td>
<td style="color: rgb(0, 0, 0); background-color: rgb(189, 215, 238);" scope="col"><span class="span_yuan"></span><span class="span_wanyuan" style="display: none;"></span></td>
<td scope="col"><span class="span_yuan"></span><span class="span_wanyuan" style="display: none;"></span></td>
<td scope="col">-</td>
<td scope="col">-</td>
</tr>
<tr>
<td scope="col">差额</td>
<td style="color: rgb(0, 0, 0); background-color: rgb(189, 215, 238);" scope="col"><span class="span_yuan"></span><span class="span_wanyuan" style="display: none;"></span></td>
<td scope="col"><span class="span_yuan"></span><span class="span_wanyuan" style="display: none;"></span></td>
<td scope="col">-</td>
<td scope="col">-</td>
</tr>
</tbody>
</table>
</div>
<div id="newData" style="display: none;"></div>

HTML导出Excel文件(兼容IE及所有浏览器)的更多相关文章

  1. 如何使用JavaScript实现前端导入和导出excel文件

    一.SpreadJS 简介 SpreadJS 是一款基于 HTML5 的纯 JavaScript 电子表格和网格功能控件,以“高速低耗.纯前端.零依赖”为产品特色,可嵌入任何操作系统,同时满足 .NE ...

  2. ExtJS Grid导出excel文件

    ExtJS Grid导出excel文件, 需下载POI:链接:http://pan.baidu.com/s/1i3lkPhF 密码:rqbg 1.将Grid表格数据连同表格列名传到后台 2.后台导出e ...

  3. PHP从数据库导出EXCEL文件

    参考博客链接:http://www.cnblogs.com/huangcong/p/3687665.html 我的程序代码 原生导出Excel文件 <?phpheader('Content-ty ...

  4. jxl导出Excel文件

    一.java项目实现读取Excel文件和导出Excel文件 实现读取和导出Excel文件的代码: package servlet; import java.io.FileInputStream; im ...

  5. PHP导出excel文件

    现在教教你如何导入excel文件: 在我的文件储存里面有一个com文件夹的,将其解压放在ThinkPHP/Library/文件夹里面,然后就是写控制器啦!去调用这个插件: <?php names ...

  6. 【转】 (C#)利用Aspose.Cells组件导入导出excel文件

    Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...

  7. PHPExcel导出excel文件

    今天园子刚开,先来个货顶下,后续园丁qing我会再慢慢种园子的,希望大家多来园子逛逛. PHPExcel导出excel文件,先说下重要的参数要记住的东西 impUser() 导入方法 exportEx ...

  8. 导出Excel文件

    /// <summary> /// 类说明:Assistant /// 更新网站:[url=http://www.sufeinet.com/thread-655-1-1.html]http ...

  9. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

随机推荐

  1. Preloading Your ASP.NET Applications

    You may have noticed that the first request to an ASP.NET Web site takes longer than subsequent requ ...

  2. 2018-2019-2 20165205 《网络对抗技术》 Exp6 信息收集与漏洞扫描

    2018-2019-2 20165205 <网络对抗技术> Exp6 信息收集与漏洞扫描 实验目标 掌握信息收集的最基本技能与常用工具的方式 实验内容 各种搜索技巧的应用 DNS IP注册 ...

  3. ORA-12560: TNS:protocol adapter error

    C:\Users\dong>sqlplus/nolog SQL*Plus: Release 11.2.0.1.0 Production on Mon Nov 19 14:12:51 2018 C ...

  4. 大量Python开源第三方库资源分类整理,含菜鸟教程章节级别链接

    Python是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明.因其具有丰富和强大的库,它常被称为胶水语言,能够把用其它语言制作的各种模块(尤其是C/C ...

  5. nginx配置多个域名

    1.原来的80端口改掉,下面配置: server { listen 80; server_name *.pobohn.com; location / { proxy_pass http://local ...

  6. Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案

    1)自定义JobFactory,通过spring的AutowireCapableBeanFactory进行注入,例如: public class MyJobFactory extends  org.s ...

  7. 微信授权获取用户openId等信息

    在我们开发小程序的时候,需要通过授权获取用户的信息. 第一种使用wx.getUserInfo直接获取微信头像,昵称 // 必须是在用户已经授权的情况下调用 wx.getUserInfo({ succe ...

  8. nginx内容

    nginx工作在7层:web server(静态内容 static contents)web reverse proxy(反向代理http,https,mail),cache(带缓存功能) proxy ...

  9. PHP:自己写的mysql操作类

    a{ font-weight: bold; display: block; text-align: center; color: #5887bf; font-size: 22px; } .conten ...

  10. Base64图片转Blob对象

    //将Base64图片转成Blob对象 //@args: base64Url:编码字符串,contentType:类型. function base64UrltoBlob(base64Url, con ...