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

    Export to excel blank page appears

    Blank page appears when data from gridview is export to excel. How to fix?
  • #767412
    hi Raj,

    if you want to export data into excel from GridView , first you have to bind data in gridview,then

    Aspx:
    add tag prefix :
    EnableEventValidation = "false" like below in first line of the aspx page
    <%@ Page Title="" Language="C#" MasterPageFile="~/Tracking/AdminSettings/AdminMaster.master" AutoEventWireup="true" CodeFile="EnquiryList.aspx.cs" Inherits="Tracking_AdminSettings_Default2" EnableEventValidation = "false" %>

    link button:

    <asp:LinkButton ID="lnkbtnExport" Text="Export to Excel" runat="server" OnClick="lnkbtnExport_Click"></asp:LinkButton>

    C # Code
    using System.Text;
    using System.IO;

    protected void lnkbtnExport_Click(object sender, EventArgs e)
    {
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Enquirers_List.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter ht = new HtmlTextWriter(sw);
    cgv.RenderControl(ht);
    Response.Write(sw.ToString());
    Response.End();
    }

    also use this in C# page

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

    hope this will help you .
    Paul.S

  • #767435
    What is your code ? there are many ways to export gridview to excel, try following sample
    Use 'HtmlTextWriter ' to export or use 'Response' object to do it, see below snippet
    here we have used interop excel object to export values, we have read each column and row values and fill them in excel object and later on we have directly open it through response object

    using (var memoryStream = new MemoryStream())
    {
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment; filename=products.xlsx");
    excel.SaveAs(memoryStream);
    memoryStream.WriteTo(Response.OutputStream);
    Response.Flush();
    Response.End();
    }

    see below link
    http://www.mikesdotnetting.com/article/278/a-better-way-to-export-gridviews-to-excel

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

  • #767497
    Hi,

    In google no of articles available for the same, please go through that articles and if you still have a queries regarding this then post your code so that we can help you better...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767505
    Hi,

    Refer the below example of Exporting Grid view to excel

    Take one dataset and in that have whatever you want to export.
    protected void ExportToExcel_Click(object sender, EventArgs e)
    {
    GridView GridView2 = new GridView();
    GridView2.AllowPaging = false;
    GridView2.DataSource = ds //ds contains data to export
    GridView2.DataBind();
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=sample.xls");
    Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    foreach (GridViewRow row in GridView2.Rows)
    {
    foreach (TableCell tc in row.Cells)
    { tc.Attributes.Add("class", "textmode"); }
    }
    GridView2.HeaderRow.BackColor = System.Drawing.Color.Aqua;
    GridView2.BackColor = System.Drawing.Color.Beige;
    GridView2.RenderControl(hw);
    string style = @"";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.Close();
    Response.End();
    }


    Hope this will help you

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #767521
    Take a look at this article

    http://www.c-sharpcorner.com/blogs/import-and-export-data-from-excel-to-database


  • Sign In to post your comments