从 javascript 或 Jquery 创建简单的 xlsx(excel 文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44404622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create simple xlsx (excel file ) from javascript or Jquery
提问by shaswatatripathy
This is the fiddle https://jsfiddle.net/ym4egje0/
这是小提琴https://jsfiddle.net/ym4egje0/
I have two things to ask
我有两件事要问
this is creating a xls , how to create a xlsx file and it does not work in IE , only works in google chrome , how to make it work in IE ?
I want to put
ColumnHeadcolumn header text and message1 and message2 should be consecutive rows .
这是创建一个 xls,如何创建一个 xlsx 文件,它在 IE 中不起作用,只能在 google chrome 中工作,如何使它在 IE 中工作?
我想把
ColumnHead列标题 text 和 message1 和 message2 应该是连续的行。
Have to take as the index of (.) to cut into rows .
必须以 (.) 的索引为切入行。
HTML
HTML
<input id="btnExport" type="button" value = "Generate File" />
JS/Jquery
JS/Jquery
$("#btnExport").click(function (e) {
var ColumnHead = "Column Header Text";
var Messages = "\n message1.\n message2.";
window.open('data:application/vnd.ms-excel,' + Messages);
e.preventDefault();
});
回答by tripathy
Check out this fiddle to solve your problem . it will create file for both Google chrome and IE
看看这个小提琴来解决你的问题。它将为 Google chrome 和 IE 创建文件
https://jsfiddle.net/shaswatatripathy/fo4ugmLp/1/
https://jsfiddle.net/shaswatatripathy/fo4ugmLp/1/
HTML
HTML
<input type="button" id="test" onClick="fnExcelReport();" value="download" />
<div id='MessageHolder'></div>
<a href="#" id="testAnchor"></a>
JS
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
CSS
.visibilityHide
{
visibility:hidden;
}

