这里是小提琴
https://jsfiddle.net/ym4egje0/13/
文件下载正在发生在IE浏览器(Internet Explorer)不适用于谷歌浏览器。
我无法更改HTML和CSS部分。
不能使用任何外部库
我想我必须改变部分
else {
$('#test').attr('href', data_type + ', ' + encodeURIComponent(tab_text));
$('#test').attr('download', 'Test file.xls');
}
但是要点击什么以便它适用于谷歌浏览器和IE浏览器
HTML
<input type="button" id="test" onClick="fnExcelReport();" value="download" />
<div id='MessageHolder'></div>
JS
function CreateHiddenTable(ListOfMessages)
{
var ColumnHead = "Column Header Text";
var TableMarkUp='<table id="myModifiedTable" class="visibilityHide"><thead><tr><td><b>'+ColumnHead+'</b></td> </tr></thead><tbody>';
console.log(ListOfMessages);
console.log(ListOfMessages.length);
for(i=0; i<ListOfMessages.length; i++){
TableMarkUp += '<tr><td>' + ListOfMessages[i] +'</td></tr>';
}
TableMarkUp += "</tbody></table>";
$('#MessageHolder').append(TableMarkUp);
}
function fnExcelReport() {
var Messages = "\n message1.\n message2.";
var ListOfMessages = Messages.split(".");
CreateHiddenTable(ListOfMessages);
var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';
tab_text = tab_text + '<x:Name>Error Messages</x:Name>';
tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';
tab_text = tab_text + "<table border='1px'>";
tab_text = tab_text + $('#myModifiedTable').html();;
tab_text = tab_text + '</table></body></html>';
var data_type = 'data:application/vnd.ms-excel';
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
if (window.navigator.msSaveBlob) {
var blob = new Blob([tab_text], {
type: "application/csv;charset=utf-8;"
});
navigator.msSaveBlob(blob, 'Test file.xls');
}
} else {
$('#test').attr('href', data_type + ', ' + encodeURIComponent(tab_text));
$('#test').attr('download', 'Test file.xls');
}
$('
#MessageHolder').html("");
}
CSS
.visibilityHide
{
visibility:hidden;
}
答案 0 :(得分:1)
encodeURIComponent
电话无法解决问题。要执行您要执行的操作,您必须Base64-encode tab_text
变量,然后将其粘贴到href属性中。 A
标记最终会如下所示:
<a href="data:application/vnd.ms-excel;base64,dmFyIHNjT2JqMSA9IG5ldyBzY3Jv...">Click here to download</a>
根据另一个SO post,您使用download
属性指定文件名是正确的。
答案 1 :(得分:1)
因此可以将其视为客户端Excel文件创建解决方案。我刚刚添加了一个元素但是如果你想要你可以在jquery中创建它并将它添加到同一个div
HTML
<input type="button" id="test" onClick="fnExcelReport();" value="download" />
<div id='MessageHolder'></div>
<a href="#" id="testAnchor"></a>
JS
var tab_text;
var data_type = 'data:application/vnd.ms-excel';
function CreateHiddenTable(ListOfMessages)
{
var ColumnHead = "Column Header Text";
var TableMarkUp='<table id="myModifiedTable" class="visibilityHide"><thead><tr><td><b>'+ColumnHead+'</b></td> </tr></thead><tbody>';
for(i=0; i<ListOfMessages.length; i++){
TableMarkUp += '<tr><td>' + ListOfMessages[i] +'</td></tr>';
}
TableMarkUp += "</tbody></table>";
$('#MessageHolder').append(TableMarkUp);
}
function fnExcelReport() {
var Messages = "\n message1.\n message2.";
var ListOfMessages = Messages.split(".");
CreateHiddenTable(ListOfMessages);
tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';
tab_text = tab_text + '<x:Name>Error Messages</x:Name>';
tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';
tab_text = tab_text + "<table border='1px'>";
tab_text = tab_text + $('#myModifiedTable').html();;
tab_text = tab_text + '</table></body></html>';
data_type = 'data:application/vnd.ms-excel';
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
if (window.navigator.msSaveBlob) {
var blob = new Blob([tab_text], {
type: "application/csv;charset=utf-8;"
});
navigator.msSaveBlob(blob, 'Test file.xls');
}
} else {
console.log(data_type);
console.log(tab_text);
$('#testAnchor')[0].click()
}
$('#MessageHolder').html("");
}
$($("#testAnchor")[0]).click(function(){
console.log(data_type);
console.log(tab_text);
$('#testAnchor').attr('href', data_type + ', ' + encodeURIComponent(tab_text));
$('#testAnchor').attr('download', 'Test file.xls');
});
CSS
.visibilityHide
{
visibility:hidden;
}