注意: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. VB 调用 webservice 出现:WSDLReader:Loading of the WSDL file failed HRESULT=0×80040154: 没有注册类别 解决方案

    有些 VB 程序在调用  webservice  的时候出现“WSDLReader:Loading of the WSDL file failed HRESULT=0×80040154: 没有注册类别 ...

  2. ECMAScript 6 新特性-set。const

    一.let命令是es6新增的特性,作用与var命令类似,声明变量,不同之处在于声明的变量的作用域为块级作用域.引入let后带来了很多新的特性. 1作用域,es5之前之后函数作用域和全局作用域,let的 ...

  3. 转载---JQuery 对 Select option 的操作

    下拉框: <select id="selectID" >         <option value="1">1</option& ...

  4. [编码实践]SpringBoot实战:利用Spring AOP实现操作日志审计管理

    设计原则和思路: 元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运营以及审计提供支持 日志记录尽可能减少性能影响 操作描述参数支持动态获取,其他参数自动记录. 1.定义日志记录元注解, ...

  5. mysql的基本操作笔记

    环境:已安装MySQL5.7.18,上一篇文章:Windows下安装MySQL5.7.18的方法 登录方式一: mysql -h 127.0.0.1 -u root -p -h:当连接MySQL服务器 ...

  6. spring boot 请求地址带有.json 兼容处理

    项目以前时spring mvc的,现在升级为spring boot ,有些请求地址带有.json后缀,在请求spring boot项目时,无法匹配控制器,spring boot默认选择禁用后缀模式匹配 ...

  7. 修改 Vultr 登录密码

    Debian,Ubuntu 访问控制台,打开在线 Console,点击右上角的 “Send CtrlAltDel”,按 ESC 键启动 GRUB boot prompt. 按 e 编辑第一启动项.按 ...

  8. 用es5原生模仿-es6Promise异步处理

    用es5原生模仿-es6Promise异步处理,不过在处理异常的时候有点小bug不是很完美,不过多级then 是没问题的和resolve, rejec  正常调用和异常处理调用是没问题的.本帖属于原创 ...

  9. 关于在centos6 + grub的旧版本中,如何关闭CPU throttling

    由于个人需求,要编译安装ATLAS库,其中就有关闭CPU throttling的步骤, 最常规简单的方法是修改grub /etc/default/grub/ 之后再接一些简单的步骤 + 重启就完成了. ...

  10. 【源】ensp 绑定本地网卡与仿真设备进行通信 场景下 仿真设备ping 不通 本机IP 问题

    环境:Win7, ensp 1.2.00.500 问题现象:本机可以ping 通 仿真设备的接口IP,但是在仿真设备上 ping不通 本机接口ip. 问题处理:把本机win7系统只带的防火墙关闭后,在 ...