Download Excel file in xlsx format using Javascript (exporting html tables into Excel) - Stack Overflow

I am using JS, HTML and CSS to build my application and host it on a server. I am using the following c

I am using JS, HTML and CSS to build my application and host it on a server. I am using the following code to import all the html tables to an excel file in xls format.

function fnExcelReport(tableNames) {
    var tab_text = "";
    var arrTableNames = tableNames.split("|");
    if (arrTableNames.length > 0) {
        for (var i = 0; i < arrTableNames.length; i++) {

                tab_text = tab_text + "<table border='1px'>";

            tab = document.getElementById(arrTableNames[i]); 


            for (j = 0; j < tab.children.length; j++) {
                tab_text = tab_text + tab.children[j].innerHTML;
            }
            tab_text = tab_text + "</table><br/><br/>";
            tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table
            tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
            tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
        }
    }
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
    {
        if (window.navigator.msSaveBlob) {
            var blob = new Blob([tab_text], {
                type: "data:application/vnd.ms-excel;"
            });
            sa = navigator.msSaveBlob(blob, "report.xls");
        }
    } else //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));

    return (sa);
}

I am able to download the file and open it, but I get the following warning message when I open it:

When I click yes, all my data is displayed correctly on the Excel file. I don't want to see that warning message. How do I change my code such that it opens with an xlsx format and removes the warning message?

I am using JS, HTML and CSS to build my application and host it on a server. I am using the following code to import all the html tables to an excel file in xls format.

function fnExcelReport(tableNames) {
    var tab_text = "";
    var arrTableNames = tableNames.split("|");
    if (arrTableNames.length > 0) {
        for (var i = 0; i < arrTableNames.length; i++) {

                tab_text = tab_text + "<table border='1px'>";

            tab = document.getElementById(arrTableNames[i]); 


            for (j = 0; j < tab.children.length; j++) {
                tab_text = tab_text + tab.children[j].innerHTML;
            }
            tab_text = tab_text + "</table><br/><br/>";
            tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table
            tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
            tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
        }
    }
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
    {
        if (window.navigator.msSaveBlob) {
            var blob = new Blob([tab_text], {
                type: "data:application/vnd.ms-excel;"
            });
            sa = navigator.msSaveBlob(blob, "report.xls");
        }
    } else //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));

    return (sa);
}

I am able to download the file and open it, but I get the following warning message when I open it:

When I click yes, all my data is displayed correctly on the Excel file. I don't want to see that warning message. How do I change my code such that it opens with an xlsx format and removes the warning message?

Share Improve this question edited Feb 23, 2017 at 19:14 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 23, 2017 at 15:13 PalakPalak 851 gold badge1 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

This is because content is not of type "xls". i.e. if excel is of type "xlsx" you will get this error. Try with other excel extensions with file name.

In your code, the blob is not Excel file. So, only change the file format. It is can not generate a correct Excel file.

There are two ways can be consideration.

  1. Use the JS Excel library. Then you just need use JS Excel library API to generate Excel.

  2. Use the Server side Excel library (such as Java, .Net server). This way need you use JS code transfer the Table data to Server, in server side generate Excel file. And then use JS code to download Excel generate in Server.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744624369a4584549.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信