Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I want to export my datatable data to excel or pdf . At any point when i click on my requirement means it should work so .

I goggled it but i was unable to find good articles which supposed to explain things in better prospective

CODE EDIT :
JavaScript
<script  type="text/javascript">

    $(document).ready(function () {

 
        $("#btnExport").click(function(e) {
            window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#myDataTable').html()));
            e.preventDefault();
        });​

        $.datepicker.regional[""].dateFormat = 'dd/mm/yy';
        $.datepicker.setDefaults($.datepicker.regional['']);

        var tab=  $('#myDataTable').dataTable({
           
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "Company/DataProvider",
            "bJQueryUI": true,
            "aoColumns": [
                         {
                             "sName": "ID" 
                         },
                         { "sName": "COMPANY_NAME" },
                         { "sName": "ADDRESS" },
                         { "sName": "TOWN" }
            ]

        });
       

        tab.makeEditable({
            "aoColumns": [
                null,
                null,
                null,
                {
                indicator: 'Saving...',
                tooltip: 'Click to select town',
                loadtext: 'loading...',
                type: 'select',
                onblur: 'submit',
                loadurl: 'AjaxDataProvider1'
            
            }]
        });

    });

        </script>

}

<table id="myDataTable" class="display">
                    <thead>

                         <tr>
                            <th>ID</th>
                            <th>Company name</th>
                            <th>Address</th>
                           <th>Town</th>
                        </tr>
    
                    </thead>
                        
                    <tbody> 
                   </tbody>

                </table>

    <input type="button" id="btnExport" value="Export to Excel" />


Well i used one of the link and tried like this I dont why same way i tried in other system its showing me some columns in excel but now its simply throwing me error like:
CHROME : Showing just table headings thats it , columns not getting loaded
IE 10 : JavaScript critical error at line 47, column 12 in http://localhost:63553/Company/Index
SCRIPT1014: Invalid character

Thing is when i removed the piece of code i done for exporting to excel the data to table getting loaded successfully .

Hoping for better suggestions

Regards
Posted
Updated 3-Jun-18 19:21pm
v2

Please see this product: http://excelbuilderjs.com[^].

If you do Web search, you will find advice to use ActiveX through the browser. Avoid it by all means. Not only it is not supported by all browsers, it is also utterly unsafe. Some security-savvy users won't come back to your site if they see that you are using such dirty approaches.

Alternatively, you should rather do the export on the server side. As you mentioned MVC, I can guess you are using .NET. If this is so, you can use Open XML SDK. Please see my past answer which references my other answers:
How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

This is another .NET-based alternative: http://npoi.codeplex.com[^].

—SA
 
Share this answer
 
Comments
Manas Bhardwaj 13-Apr-14 16:19pm    
+5!

See my answer. Some of them are quite simple and impressive.
Sergey Alexandrovich Kryukov 13-Apr-14 16:29pm    
Thank you, Manas.
—SA
sunil gutta 14-Apr-14 7:22am    
Hey i heard table tools will working fine which provides to export to different sources excel , pdf etc . but i am unable to understand things clearly . any opinion mates
Sergey Alexandrovich Kryukov 14-Apr-14 10:43am    
Every activity assume some required prerequisites. Asking questions, too. It is assumed that you are able to understand thing clearly, and, in case of some unclear items, are able to ask clear questions on them. And so on...
—SA
sunil gutta 14-Apr-14 12:43pm    
ok mate i will . and sorry for inconvenience provided and i will look into the links you given . tried one of those links , static html table content wont help me much but i tried that option on my jquery datatable i am getting just columns but no records .
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Apr-14 16:29pm    
Sure, 5ed.
—SA
I couldn't look into your code but the below should work.. replace #btnGuru with your button name.


JavaScript
$("#btnGuru").click(function () {
    tableToExcel('myDataTable', 'W3C Example Table');
});


var tableToExcel = (function () {
    var uri = 'data:application/vnd.ms-excel;base64,'
      , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
      , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
      , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
    return function (table, name) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
        window.location.href = uri + base64(format(template, ctx))
    }
})()
 
Share this answer
 
Comments
sunil gutta 16-Apr-14 1:19am    
The best solution so far :) yes i am getting data but you know i been working with plugins mostly i tought to try with table tools plugin which gives more options to deal things . xls,pdf,print etc . So have you came across TABLE TOOLS .. i am trying on that i am facing display and otehr issues too
Guruprasad.K.Basavaraju 16-Apr-14 1:31am    
No, what plugin are you working with ? Also please close this question by accepting one of the answers above. The solutions above mine are also correct, please accept those too. We can look at your plugins in another thread.
sunil gutta 16-Apr-14 16:09pm    
Guru when you get time please look into the question i posted on table tools .. everything i am doing perfectly but some where something screwing
sunil gutta 16-Apr-14 3:11am    
ok i will post a new question exclusively on datatools plugin .. thank you . possible look into my new post with my work on code mentioned : CLICK HERE

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