Tuesday, 7 July 2015

Export table data to Excel and MS word in Jquery

Export to Excel:

html table which contains data as shown below.

<table id="tblviewtimesheet">
    <thead>
        <tr>
           
            <th>ID</th>
            <th>Emp Name</th>
            <th>Project Name</th>
            <th>Salary</th>
            <th>address</th>
            <th>designation</th>
        </tr>
    </thead>
    <tbody>
        <tr data-ng-repeat="emp in employeess[currentPage]">
         
            <td>
                <span>{{emp.Id}}</span>
            </td>
           
            <td>
                <span>{{emp .EmployeeName}}</span>
            </td>
            <td>
                <span>{{emp .Project}} ({{emp .ProjectCode}})</span>
            </td>
           
            <td>
                <span>{{emp .Salary}}</span>
            </td>
            <td>
                <span>{{emp .address}}</span>
            </td>
            <td>
                
               <span>{{emp.designation}}</span>
            </td>
            
        </tr>
    </tbody>

</table>



Script to export above table to Excel:


take one button in html write a on click function as shown



function fnExcelReport() {

    tab = document.getElementById('tblviewtimesheet');


    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
    {
        txtArea1.document.open("txt/html", "replace");
        txtArea1.document.write(tab);
        txtArea1.document.close();
        txtArea1.focus();
        sa = txtArea1.document.execCommand("SaveAs", true, "ViewTimesheet.xls");
    }
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab), '_self');
    return (sa);
}


Script to export to Ms word:

   function fnWordreport() {

    var tab_text = "<table border='2px'><tr bgcolor='#827066'>";
    var textRange; var j = 0;
    tab = document.getElementById('tblreportdetails'); // id of table
    for (j = 0 ; j < tab.rows.length ; j++) {
        tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
        //tab_text=tab_text+"</tr>";
    }

    tab_text = tab_text + "</table>";
    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
    {
        txtArea1.document.open("txt/html", "replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus();
        sa = txtArea1.document.execCommand("SaveAs", true, "Reportdetails.doc");
    }
    else                 //other browser not tested on IE 11
        sa = window.open('data: application/msword,' + encodeURIComponent(tab_text), '_self');

    return (sa);
}

No comments:

Post a Comment