none
how to export a dynamically generated datatable into excel sheet 2013? RRS feed

  • Question

  • I have one data table and in that data table i am fetching data from sharepoint 2013 lists.

    When I export that data table into excel sheet, what ever data is static will be shown but the data which I fetch from lists are not coming.

    How should I get that data in my excel sheet from that data table?

    This is the code for Export to Excel:

    public void ExportToExcel(DataTable dt)
            {
                if (dt.Rows.Count > 0)
                {
                    string filename = "WebTrafficReport.xls";
                    System.IO.StringWriter tw = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                    DataGrid dgGrid = new DataGrid();
                    dgGrid.DataSource = dt;
                    dgGrid.DataBind();
    
                    //Get the HTML for the control.
                    dgGrid.RenderControl(hw);
                    //Write the HTML back to the browser.
                    //Response.ContentType = application/vnd.ms-excel;
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                    this.EnableViewState = false;
                    Response.Write(tw.ToString());
                    Response.End();
                }
            }

    Thanks in Advance.

    Tuesday, October 7, 2014 9:06 AM

Answers

All replies

  • ublic void ExportToExcel(DataTable dt)
            {
                if (dt.Rows.Count > 0)
                {
                    string filename = "WebTrafficReport.xls";
                    System.IO.StringWriter tw = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                    DataGrid dgGrid = new DataGrid();
                    dgGrid.DataSource = dt;
                    dgGrid.DataBind();
    
                    //Get the HTML for the control.
                    dgGrid.RenderControl(hw);
                    //Write the HTML back to the browser.
                    //Response.ContentType = application/vnd.ms-excel;
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                    this.EnableViewState = false;
                    
    HttpContext.Current.Response.Write(sw.ToString());
                        HttpContext.Current.Response.Flush();
                        HttpContext.Current.Response.End();
                

    }}

    try this should help

    Tuesday, October 7, 2014 1:56 PM
  • Hi,

    According to your description, you want to export some list items to Excel file programmatically.

    Would you mind providing more details about “When I export that data table into excel sheet, what ever data is static will be shown but the data which I fetch from lists are not coming”? I’m not quite clear about what you really mean.

    Anyway, here are two demos about export list items to Excel file with code snippets provided would be helpful:

    http://www.etechplanet.com/blog/export-sharepoint-list-to-excel-spreadsheet-programmatically-using-c.aspx

    http://magnustechnologyprimus.blogspot.com/2013/12/export-sharepoint-list-to-excel.html

    Best regards,

    Patrick

    Patrick Liang
    TechNet Community Support

    Wednesday, October 8, 2014 6:54 AM
    Moderator