gpt4 book ai didi

javascript - 从 javascript 或 Jquery 创建简单的 xlsx(excel 文件)

转载 作者:行者123 更新时间:2023-11-28 11:21:18 25 4
gpt4 key购买 nike

这是 fiddle https://jsfiddle.net/ym4egje0/

我有两件事想请教

  1. 这是创建一个xls,如何创建一个xlsx文件,它在IE中不起作用,只能在google chrome中起作用,如何使其在IE中起作用?

  2. 我想放置 ColumnHead 列标题文本,并且 message1 和 message2 应该是连续的行。

必须以(.)为索引来切行。

HTML

<input id="btnExport" type="button" value = "Generate File" />

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();

});

最佳答案

看看这个 fiddle 来解决您的问题。它将为 Google Chrome 和 IE 创建文件

https://jsfiddle.net/shaswatatripathy/fo4ugmLp/1/

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;
}

关于javascript - 从 javascript 或 Jquery 创建简单的 xlsx(excel 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44404622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com