<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HTML表格导出到EXCEL</title>
<script type="text/javascript" language="javascript">
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 table2excel(tableid)
{ //整个表格拷贝到EXCEL中
if (getExplorer() == 'ie')
{
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel
var oWB = oXL.Workbooks.Add();
//获取workbook对象
var xlsheet = oWB.Worksheets(1);
//激活当前sheet
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
//把表格中的内容移到TextRange中
sel.select();
//全选TextRange中内容
sel.execCommand("Copy");
//复制TextRange中内容
xlsheet.Paste();
//粘贴到活动的EXCEL中
oXL.Visible = true;
//设置excel可见属性 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);
//xls.visible = false;
oXL.Quit();
oXL = null;
//结束excel进程,退出完成
//window.setInterval("Cleanup();",1);
idTmr = window.setInterval("Cleanup();", 1); } }
else
{
tableToExcel(tableid)
}
}
function Cleanup()
{
window.clearInterval(idTmr);
CollectGarbage();
}
var tableToExcel = (function ()
{
var uri = 'data:text/xls;charset=utf-8,\ufeff,',
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]--></head><body><table>{table}</table></body></html>',
base64 = function (s)
{
return window.btoa(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)) var downloadLink = document.createElement("a");
downloadLink.href = uri + format(template, ctx);
downloadLink.download = '统计.xls';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
)() </script> </head>
<body>
<table id="targetTable" cellpadding="0" cellspacing="0" border="1">
<tr align="center">
<th>第一列</th>
<th>第二列</th>
<th>第三列</th>
</tr>
<tr align="center">
<td>a</td>
<td>b</td>
<td rowspan="2" style="border:0">
<table width="100%" cellpadding="0" cellspacing="0" border="1">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td rowspan="2">3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
</tr>
</table>
</td>
</tr>
<tr align="center">
<td>c</td>
<td>d</td>
</tr>
</table>
</br>
<input id="Button1" type="button" value="导出EXCEL" onclick="javascript:table2excel('targetTable')" />
</body>
</html>

将HTML表格导出到EXCEL,兼容Firefox,支持中文的更多相关文章

  1. JS前端将table导出到excel 兼容谷歌 IE 且保留表格样式

    CDSN上博主给我一段代码,可将表格导出为EXCEL文档,原文见: https://blog.csdn.net/zz210891470/article/details/94717644 向博主学习.致 ...

  2. 网站开发进阶(二十五)js如何将html表格导出为excel文件

    js如何将html表格导出为excel文件        赠人玫瑰,手留余香.若您感觉此篇博文对您有用,请花费2秒时间点个赞,您的鼓励是我不断前进的动力,共勉! jsp页面数据导出成excel的方法很 ...

  3. 纯JS 将table表格导出到excel

    html <div > <button type="button" onclick="getXlsFromTbl('tableExcel','myDiv ...

  4. 使用JavaScript把页面上的表格导出为Excel文件

    如果在页面上展示了一个数据表格,而用户想把这个表格导出为Excel文件,那么在要求不高的情况下,可以不通过服务器生成表格,而是直接利用JavaScript的Blob和Object URL特性将表格导出 ...

  5. JS 将页面上的表格导出为 Excel 文件

    如果在页面上展示了一个表格,想把这个表格导出为Excel文件,那么在要求不高的情况下,可以直接利用 JavaScript 的 Blob 和 Object URL 特性将表格导出.不过,这就是利用了 E ...

  6. 网站开发进阶(三十一)js如何将html表格导出为excel文件(后记)

    js如何将html表格导出为excel文件(后记) 前言 项目前期做了个导出Excel表格的功能,但是经过测试发现只有在IE上才可以正确实现,在Chrome等浏览器中无法实现导出效果.经过上网搜索,尝 ...

  7. vue将表格导出为excel

    vue将表格导出为excel 一:在项目中需要安装2个依赖项,如下命令: npm install --save file-saver xlsx 二:在vue文件中如下使用即可: <templat ...

  8. 表格导出到excel的样式消失该如何修改

    工作中遇到一需求,要将后台的表格导出到excel后的表格样式该如何修改呢? 其实表格导出首先需要了解两个插件:jquery.table2excel.js和tableExport.js 1.第一步,写一 ...

  9. 将表格导出为excel

    <table id="tableExcel" border="1"> <tr> <th>零</th> <t ...

随机推荐

  1. 树莓pi快速ssh【校园网】

    校园网是NAT后的,没有显示器,我把路由器的DHCP服务关了,这样的情况下怎么ssh到树莓pi? 把树莓pi 连到路由器上(已经关闭DHCP),手机安装FING https://play.google ...

  2. hive 配置mysql元数据库

    在 hive的配置文件hive-site.xml中 <?xml version="1.0"?> <!-- Licensed to the Apache Softw ...

  3. 从IRP说起(转)

    原文链接:http://www.cnblogs.com/zhuyp1015/archive/2012/03/14/2396595.html IRP(I/O request package)是操作系统内 ...

  4. 2016"百度之星" - 初赛(Astar Round2B) 1006 中位数计数

    思路:统计当前数左边比它小|大 i个人,相应右边就应该是比它大|小i个人 l数组表示左边i个人的方案 r表示右边i个人的方案 数组下标不可能是负数所以要加n //#pragma comment(lin ...

  5. C#通过DllImport引入dll中的C++非托管类(转)

    http://blog.sina.com.cn/s/blog_70a144580100tmj8.html

  6. 【Spark学习】Apache Spark项目简介

    引言:本文直接翻译自Spark官方网站首页 Lightning-fast cluster computing 从Spark官方网站给出的标题可以看出:Spark——像闪电一样快的集群计算 Apache ...

  7. Android开发--ListPreferance 运行报错:android.preference.ListPreference.findIndexOfValue(ListPreference.java:169)

    在Stack Overflow上找到的答案:http://stackoverflow.com/questions/4357094/exception-on-listpreferences “i fix ...

  8. css中文字体unicode对照表

    为什么要使用Unicode编码代替中文字体 在CSS中使用中文字体通常直接设置字体名称,比如设置字体为宋体:font-family:’宋体’:但因此产生的一个问题是,如果默认编码并不是UTF-8,这会 ...

  9. eclipse反编译插件

    eclipse翻译的插件很多,jadclipse 是其中一款.设置很简单. ①.下载net.sf.jadclipse_3.3.0.jar 官方网站:http://sourceforge.net/pro ...

  10. 对PostgreSQL的prepared statement的深入理解

    看官方文档: http://www.postgresql.org/docs/current/static/sql-prepare.html PREPARE creates a prepared sta ...