【转载】JavaScript导出Excel
【转载】JavaScript导出Excel
如果没有用到前端插件,也没有用到后台poi导出的话,用js导出也是一种方式。亲测可用。
/**
* 导出excel
*/
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 exportExcel(tableid) {
if(getExplorer()=='ie')
{
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var xlsheet = oWB.Worksheets(1);
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
sel.select();
sel.execCommand("Copy");
xlsheet.Paste();
oXL.Visible = true;
try {
var fname = oXL.Application.GetSaveAsFilename("Excel.xls",filename);
} catch (e) {
print("Nested catch caught " + e);
} finally {
oWB.SaveAs(fname);
oWB.Close(savechanges = false);
oXL.Quit();
oXL = null;
idTmr = window.setInterval("Cleanup();", 1);
}
}
else
{
tableToExcel(tableid);
}
}
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
} var tableToExcel = (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]-->'+
' <style type="text/css">'+
'.excelTable {'+
'border-collapse:collapse;'+
' border:thin solid #999; '+
'}'+
' .excelTable th {'+
' border: thin solid #999;'+
' padding:20px;'+
' text-align: center;'+
' border-top: thin solid #999;'+
' background-color: #E6E6E6;'+
' }'+
' .excelTable td{'+
' border:thin solid #999;'+
' padding:2px 5px;'+
' text-align: center;'+
' }</style>'+
'</head><body ><table class="excelTable">{table}</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) ;
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} ;
// window.location.href = uri + base64(format(template, ctx)) ;
document.getElementById("exportExcel").href = uri + base64(format(template, ctx));
document.getElementById("exportExcel").download = filename;
// document.getElementById("exportExcel").click();
} ;
})() ; function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
【转载】JavaScript导出Excel的更多相关文章
- [转载]poi导出excel,可以自定义保存路径
poi导出excel比js导出excel安全性更好,在使用poi导出excel时,先要导入poi-3.5-FINAL-20090928.jar包到你项目的lib目录下,我这里选择是3.5版的 1.ac ...
- 原生JavaScript 导出excel表格(兼容ie和其他主流浏览器)
因同事的需求是想前端导出excel表格,网上找了一些demo,自己修改了一下,可能以后会用到,记录下来吧,兼容ie和一些主流浏览器,ie可能会报错,原因参考 这里,edge 浏览器还没有办法导出,正在 ...
- [转载]Java导出Excel
一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...
- JavaScript 导出Excel 代码
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- JavaScript导出excel文件实现
function getXlsFromTbl(inTblId, inWindow, fname) { try { var allStr = ""; var curStr = &qu ...
- javascript 导出Excel
测试兼容IE google 火狐浏览器.看到的朋友也许你某一天也会需要. //obj是table表格外面嵌套div id function saveCode(obj) { try { var strH ...
- 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel
步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript&qu ...
- html中的table导出Excel
演示地址: http://www.jq22.com/yanshi3312 具体代码: <!DOCTYPE html> <html lang="zh-CN"> ...
- html中的table导出Excel (亲测有用(●'◡'●))
演示地址: http://www.jq22.com/yanshi3312 具体代码: <!DOCTYPE html> <html lang="zh-CN"> ...
随机推荐
- BootStrap:
BootStrap: * 响应式的HTML,CSS,JS的框架. * 响应式设计: * 设计一套页面,适配不同的设备,在手机,PAD,PC端都能够正常浏览. * 响应式原理: * 使用CSS3的媒体查 ...
- Junit 单元测试、BeanUtils、Properties类
一. Junit单元测试 1.1. Junit单元测试框架的基本使用 一.搭建环境: 导入junit.jar包(junit4) 二.写测试类: 0,一般一个类对应一个测试类. 1,测试类与被测试类最好 ...
- SharePoint 2013 - REST API about Security
1. 获取当前用户信息(current user): var currentUserInfo = "{0}/_api/Web/CurrentUser"; // {0} -> ...
- Android Studio图形基础(AS开发实战第二章学习笔记)
图形基础 一.drawable 在代码中引用drawable文件可分为两种情况 (1)使用setBackgroundResource和setImageResource方法,可直接在参数中指定drawa ...
- unity配置Android SDK,并构建导出apk格式
1.点击 Edit --> preferences出现如图界面 2.在Android中SDK选项中放入Android SDK:JDK选项中放入Java JDK.(下载地址:http://t ...
- IE的兼容性设置 X-UA-Compatible
< meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" /> 是IE8 ...
- 关于Flume以及Kafka理解
- 64位Navicat Premium-11.2.7(64bit)访问64位Oracle服务器
1 在windows 10 64位操作系统安装Navicat Premium-11.2.7(64bit). 2 在服务器安装64位的Oracle(win64_11gR2_database). 3 在h ...
- ajax post data 获取不到数据,注意content-type的设置post/get
因为之前一直用jQuery ajax get的方式传递参数, 默认没有设置过 contentType 的值. $.ajax({ url: "/yuanjin/jianxiang", ...
- 【Leetcode】【Medium】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...