Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to give Download Excel button this table i want in excel

Internet exploer extension not coming

here ASHA,ANM,Mother is dynamically coming from db

HTML
<table class="table table-bordered">
                                                <thead>
                                                    <tr class="tblhead">
                                                        <th>State Name</th>
                                                            <th colspan="2">Mother</th>
                                                            <th colspan="2">ASHA</th>
                                                            <th colspan="2">ANM</th>

                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td> </td>
                                                            <td>Count</td>
                                                            <td>Payment</td>
                                                            <td>Count</td>
                                                            <td>Payment</td>
                                                            <td>Count</td>
                                                            <td>Payment</td>

                                                    </tr>
                                                     
                                                         <tr>
                                                        <td width="20%"><a>Andhra Pradesh</a></td>
                                                                <td width="10%">22</td>
                                                                <td width="10%">₹ 30,800</td>
                                                                <td width="10%">57</td>
                                                                <td width="10%">₹ 12,450</td>
                                                                <td width="10%">32</td>
                                                                <td width="10%">₹ 7,250</td>
                                                             </tr>
                                                         <tr>
                                                        <td width="20%"><a>Arunachal Pradesh</a></td>
                                                                <td width="10%">24</td>
                                                                <td width="10%">₹ 54,545</td>
                                                                <td width="10%">12</td>
                                                                <td width="10%">₹ 65,655</td>
                                                                <td width="10%">23</td>
                                                                <td width="10%">₹ 89,815</td>
                                                             </tr>
                                                         <tr>
                                                        <td width="20%"><a>Assam</a></td>
                                                                <td width="10%">45</td>
                                                                <td width="10%">₹ 304,800</td>
                                                                <td width="10%">56</td>
                                                                <td width="10%">₹ 12,450</td>
                                                                <td width="10%">56</td>
                                                                <td width="10%">₹ 7,250</td>
                                                             </tr>

                                                </tbody>
                                            </table>


What I have tried:

JavaScript
function fnExcelReport() {
        var tab_text = "<table border="2px"><tr bgcolor="#87AFC6">";
        var textRange; var j = 0;
        tab = document.getElementById('tblTesting'); // id of table

        for (j = 0 ; j < tab.rows.length ; j++) {
            tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
            //tab_text=tab_text+"";
        }

        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, "Activity.xls");
            return (sa);
        }
        else {
            //other browser not tested on IE 11
            //sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
            var a = document.createElement('a');
            a.href = 'data:application/vnd.ms-excel,' + encodeURIComponent(tab_text);
            a.download = 'exported_table.xls';
            a.click();
            e.preventDefault();
        }
        
    }
Posted
v4
Comments
[no name] 15-Mar-16 6:11am    
What?
Sinisa Hajnal 15-Mar-16 11:24am    
Better solution would be to go to the server, get the data you need in proper format or provide the file to export via some report viewer.
Vikram Dalwadiya 18-Mar-16 9:28am    
https://jsfiddle.net/lesson8/jWAJ7/
Mano N 2-Apr-16 4:35am    
Better use Ajax Webmethod

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900