注意: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. 刘志梅201771010115.《面向对象程序设计(java)》第十四周学习总结

    实验十四  Swing图形界面组件 实验时间 20178-11-29 1.实验目的与要求 (1)设计模式:反复使用.经过分类编目的.代码设计经验的总结. 在Alexander的模式分类和软件模式的分类 ...

  2. 学习MeteoInfo二次开发教程(十)

    1.复制文件cloud_1009271330.000,I-01.pal 2.改: GridData cloud = aDataInfo.GetGridData(""); 为: Gr ...

  3. python 常用技巧

    一.字符串与数值的转换 Python中字符串转换为数值: str_num = '99' num = int(str_num) 整型数转换为字符串: num = 99 str_num = str(num ...

  4. Python课程第四天作业

    1.定义一个函数,该函数可以实现控制台输入,最终返回一个int类型的正整数 def chr_to_int(): user_input = input('请输您要转换的字符串:') if user_in ...

  5. MSMQ 队列消息和死信队列

    日志消息 日志队列可以保存你操作过的消息的备份.它的好处是,一但发现前面的操作失败,可以从日志队列中重新创建出原先的消息对象,然后再进行操作. 例如,向远方发送一个消息对象,然后对方返回一个失败的确认 ...

  6. 前端生成excel

    一:安装依赖 1:在package.json 中加入四个依赖包 (1): "json2csv": "^3.11.0", (2):"file-saver ...

  7. C++中#include<iostream>

    #include 是个包含命令,就是把iostream.h这个文件里的内容复制到这个地方 iostream.h是input output stream的简写,意思为标准的输入输出流头文件.它包含: ( ...

  8. ORM版学员管理系统 2

    学生信息管理 展示学生信息 URL部分 url(r'^student_list/', app01_views.student_list, name="student_list"), ...

  9. Spring中@Value注解使用——一种新的直接读取配置文件的方式

    1.@Value注解作用 该注解的作用是将我们配置文件的属性读出来,有@Value(“${}”)和@Value(“#{}”)两种方式. 2.@Value注解作用的两种方式 场景 假如有以下属性文件de ...

  10. Java数据类型(Primivite 和引用数据类型!)

    一.byte(8位)  short(16位)  int(32位) long(64位) float(32位) double(64位) boolean(Java虚拟机决定)  true 或者false! ...