最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create simple xlsx (excel file ) from javascript or Jquery - Stack Overflow

programmeradmin4浏览0评论

This is the fiddle /

I have two things to ask

  1. 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 ?

  2. I want to put ColumnHead column header text and message1 and message2 should be consecutive rows .

Have to take as the index of (.) to cut into rows .

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

});

This is the fiddle https://jsfiddle/ym4egje0/

I have two things to ask

  1. 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 ?

  2. I want to put ColumnHead column header text and message1 and message2 should be consecutive rows .

Have to take as the index of (.) to cut into rows .

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

});
Share Improve this question asked Jun 7, 2017 at 5:58 shaswatatripathyshaswatatripathy 1711 gold badge5 silver badges13 bronze badges 6
  • for xlsx use data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet I hope it will work. – acmsohail Commented Jun 7, 2017 at 6:12
  • now it downloading without any file extension . how to make .xlsx and also can i give any specific name other than download – shaswatatripathy Commented Jun 7, 2017 at 6:20
  • Can you try this post? stackoverflow./questions/15567086/… – M Thomas Commented Jun 7, 2017 at 6:25
  • Its for Table , I dont need table that big and IE solution does not work for me – shaswatatripathy Commented Jun 7, 2017 at 6:29
  • It download the file extension in chrome jsfiddle/ym4egje0/1 Check similar question from stackoverflow./questions/2937465/… – acmsohail Commented Jun 7, 2017 at 6:29
 |  Show 1 more ment

1 Answer 1

Reset to default 11

Check out this fiddle to solve your problem . it will create file for both Google chrome and IE

https://jsfiddle/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-: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;
}
发布评论

评论列表(0)

  1. 暂无评论