You must Sign In to post a response.
  • Category: ASP.NET

    Export gridview to excel using javascript?

    How to export gridview to excel using Javascript without using Active X control???


    If it is not possible, can u give me reason??
  • #749029
    Yes it is possible , first bind your data in HTML table or Div then you can export it ,Refer these links.

    http://stackoverflow.com/questions/22317951/export-html-table-data-to-excel-using-javascript-jquery-is-not-working-properl

    Demo

    http://jsfiddle.net/terryyounghk/KPEGU/

    http://blog.eliacontini.info/post/79860720828/export-to-csv-using-javascript-the-download-attribute

    Regards,
    Nirav Prabtani (Senior Web Developer)
    Email : niravjprabtani@gmail.com
    blog : niravprabtani.blogspot.in

  • #749036
    It is a bad idea to export data to an Excel file using this method. It will export to older format of Excel.

    Bind all GridView data to DIV and then extract the contents of the DIV element using JQuery.
    Check this.
    http://www.encodedna.com/2014/02/get-all-child-div-ids-inside-a-div-using-jquery.htm

    Finally export the data to an Excel file using this JQuery procedure.

    <script>
    $(document).ready(function() {
    $('#ExportData').click(function(e) {
    window.open('data:application/vnd.ms-excel,' + $('#showChild').html());
    e.preventDefault();
    });

    });
    </script>

    Hope this helps.

  • #749069
    if you don't want to use 'ActiveXcontrol' then you can go for JQuery, following link will help you export the content of gridview to excel file
    http://wsnippets.com/export-html-table-data-excel-sheet-using-jquery/
    The above link contains jquery plugin to convert html table content into excel sheet. It is very simple to use. It supports not only table data also for data grids, from XML file or Json files.
    it helps to
    1. Export Data grid to excel sheet
    2. Export to excel sheet from external JSON

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #749074
    Hi,

    You can achieve this as prasad explained above. But why don't you give a try using simple asp.net. .NET have very good features for microsoft related stuffs. using simple controls you can achieve this. Below is the code for importing a grid in excel-
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "demo.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gvDetails.AllowPaging = false;
    BindGridview();
    gvDetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
    for (int i = 0; i < gvDetails.HeaderRow.Cells.Count; i++)
    {
    gvDetails.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
    }
    gvDetails.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #749088
    Instead of using java script better use JQuery code for export option in grid.

    here is a sample code for the same.



    $("[id$=Button1]").click(function(e) {
    window.open('data:application/vnd.ms-excel,' + encodeURIComponent( $('div[id$=dvdata]').html()));
    e.preventDefault();
    });

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM


  • Sign In to post your comments