导出到excel不能在Internet Explorer 11中工作
Export to excel not working in Internet Explorer 11
嗨,我使用这段代码在我的HTML表上导出excel,但它不能在IE 11中工作。有人能帮帮我吗?
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]--></head><body><table>{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))
}
})()
这行得通:
<a table_div='ExportcommunicationPlanDiv' class="btnExport"><li title="Export to Excel"><div class="btn-excel"> </div></li></a>
<script>
var excelfileName="download";
$(".btnExport").click(function () {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd;
}
if(mm<10){
mm='0'+mm;
}
var today = dd+'/'+mm+'/'+yyyy;
excelfileName = $('#POTS_CKT_ID').val() +"_"+today;
// var todaysDate = moment().format('DD-MM-YYYY');
if(isIE())
{
var id = $(this).attr('table_div');
var blobURL = tableToExcelForIE(''+id+'');
}
else
{
//var todaysDate ="test";
var id = $(this).attr('table_div');
var blobURL = tableToExcel(''+id+'');
$(this).attr('download',excelfileName+'.xls')
$(this).attr('href',blobURL);
}
});
function isIE()
{
var isIE11 = navigator.userAgent.indexOf(".NET CLR") > -1;
var isIE11orLess = isIE11 || navigator.appVersion.indexOf("MSIE") != -1;
return isIE11orLess;
}
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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{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}
var blob = new Blob([format(template, ctx)]);
var blobURL = window.URL.createObjectURL(blob);
return blobURL;
}
})()
var tableToExcelForIE = (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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{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}
var blob = new Blob([format(template, ctx)]);
var blobURL = window.URL.createObjectURL(blob);
//IE
if (isIE()) {
csvData = table.innerHTML;
if (window.navigator.msSaveBlob) {
var blob = new Blob([format(template, ctx)], {
type: "text/html"
});
navigator.msSaveBlob(blob, ''+excelfileName+'.xls');
}
}
}
})()
</script>
It is Working Good .
in html code
<a table_div='ExportcommunicationPlanDiv' class="btnExport"><li title="Export to Excel"><div class="btn-excel"> </div></li></a>
<script>
var excelfileName="download";
$(".btnExport").click(function () {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd;
}
if(mm<10){
mm='0'+mm;
}
var today = dd+'/'+mm+'/'+yyyy;
excelfileName = excelfileName +"_"+today;
// var todaysDate = moment().format('DD-MM-YYYY');
if(isIE())
{
var id = $(this).attr('table_div');
var blobURL = tableToExcelForIE(''+id+'');
}
else
{
//var todaysDate ="test";
var id = $(this).attr('table_div');
var blobURL = tableToExcel(''+id+'');
$(this).attr('download',excelfileName+'.xls')
$(this).attr('href',blobURL);
}
});
function isIE()
{
var isIE11 = navigator.userAgent.indexOf(".NET CLR") > -1;
var isIE11orLess = isIE11 || navigator.appVersion.indexOf("MSIE") != -1;
return isIE11orLess;
}
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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{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}
var blob = new Blob([format(template, ctx)]);
var blobURL = window.URL.createObjectURL(blob);
return blobURL;
}
})()
var tableToExcelForIE = (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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{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}
var blob = new Blob([format(template, ctx)]);
var blobURL = window.URL.createObjectURL(blob);
//IE
if (isIE()) {
csvData = table.innerHTML;
if (window.navigator.msSaveBlob) {
var blob = new Blob([format(template, ctx)], {
type: "text/html"
});
navigator.msSaveBlob(blob, ''+excelfileName+'.xls');
}
}
}
})()
</script>
相关文章:
- 视频HTML没有'无法在Internet Explorer 11上工作
- 为什么我的JavaScript / JSON脚本不能在Internet Explorer中工作
- $.getJSON 和 google fonts API 停止在 Internet Explorer 中工作,jQuer
- 我正在尝试创建一个可以在Internet Explorer(Windows 10,又名Edge)中工作的html语音到文
- 得到"标签“;标签在Internet Explorer 6中工作(用于单选按钮)
- Internet Explorer跨域请求无法正常工作,MS没有给出错误的理由
- 将DIV导出为PDF don'无法在Internet Explorer中工作
- Chrome 和 Internet Explorer 无法离线工作,在 Firefox 上工作正常
- Jquery 无法在 Internet Explorer 中正常工作
- 使用jQuery的Internet Explorer上的Javascript错误,但在Firefox上工作正常
- 我的代码在Firefox中工作,但不能在Internet Explorer中工作
- Ajax 无法在 Internet Explorer 中正常工作
- Ajax 在 Internet Explorer 9 中更改域名后停止工作
- 如何才能让响应式幻灯片在Internet Explorer中工作
- Javascript 3D 轮播在 Internet Explorer 中无法正常工作
- React无法在Internet Explorer中工作
- HTML5<音频>标记donesn'不能在Firefox和Internet Explorer上工作
- 登录按钮和身份验证可以在Internet explorer中工作
- Javascript this.firstChild.nextSibling在Internet Explorer中不工作
- Ajax iframe请求在Internet Explorer中只能工作一次