Export HTML table to Excel JavaScript function add select file name

前端 未结 2 1186
感情败类
感情败类 2021-02-13 13:15

I have the following function that exports an HTML to excel:

function generateexcel(tableid) {
  var table= document.getElementById(tableid);
  var html = table.         


        
相关标签:
2条回答
  • 2021-02-13 13:40

    There are two options which you could look into:

    • Filesaver API is new 'HTML5' functionality allowing /exactly/ this. There is just one small problem: the relevant part isn't supported yet in firefox. If you want to use this there is a nice wrapper library which makes this easier for you: filesaver.js
    • Downloadify is a flash tool which is created for exactly this as well, you can find it here. ('Disadvantage': flash)
    0 讨论(0)
  • 2021-02-13 13:47

    I'm not sure if you have done this already. You might need to handle something like this below in your aspx page:

    $(window).load(function(){
    $( "#clickExcel" ).click(function() {  
    var dtltbl = $('#dtltbl').html();    `enter code here`
    window.open('data:application/vnd.ms-excel,' + $('#dtltbl').html());
    });
    });//]]>  
    

    In the above script #dtltbl is the Table Id.

    The following code needs be there in your server side code, then your problem would be solved.

               Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
    
    0 讨论(0)
提交回复
热议问题