2015年10月29日 星期四

JQuery: export Table to Excel file

Method 1

To export specific table with certain tableID:

$("#excel_icon").click(function(e) {
     
            var uri = 'data:application/vnd.ms-excel,' + encodeURIComponent($('#divThatEncloseTargetTable').html());
            var downloadLink = document.createElement("a");
            downloadLink.href = uri;
            downloadLink.download = "report.xls";   //set file name
            document.body.appendChild(downloadLink);
            downloadLink.click();
            document.body.removeChild(downloadLink);
            e.preventDefault();
        });

Method 2

Set contentType & header for the whole page:
response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "inline; filename="

            + "excel.xls");

沒有留言:

張貼留言