Export to excel is not working from web user control's gridview [Resolved]

Posted by Santosh4u under ASP.NET on 3/12/2016 | Points: 10 | Views : 484 | Status : [Member] | Replies : 1
Hi,
i want export gridview data to excel,i am trying to export and it's working in aspx page but when i am using same code in web user control(ascx) page , it's not working.

even i tried to override VerifyRenderingInServerForm in my aspx page where i am binding user control also not working.
public override void VerifyRenderingInServerForm(Control control) { }

refer the below where i am doing wrong.let me know if have any other idea to export to excel/pdf

protected void ExporttoExcel_Click(object sender, EventArgs e)
{
ExporttoExcel();
}
private void ExporttoExcel()
{
BindGridDetails();
HtmlForm form = new HtmlForm();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "Student.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
// BindGridDetails(GridView1);
form.Attributes["runat"] = "server";
form.Controls.Add(GridView1);
this.Controls.Add(form);
form.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@;}</style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
protected void BindGridDetails()
{
DataTable dt = new DataTable();
dt.Columns.Add("StudentID", typeof(Int32));
dt.Columns.Add("Student_Name", typeof(string));
dt.Columns.Add("City", typeof(string));
DataRow dtrow = dt.NewRow();
dtrow["StudentID"] = 1;
dtrow["Student_Name"] = "Rakesh";
dtrow["City"] = "Delhi";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();
dtrow["StudentID"] = 2;
dtrow["Student_Name"] = "Suresh";
dtrow["City"] = "Mumbai";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();
dtrow["StudentID"] = 3;
dtrow["Student_Name"] = "Samir";
dtrow["City"] = "Bangalore";
dt.Rows.Add(dtrow);

GridView1.DataSource = dt;
GridView1.DataBind();

}
protected void BindingGridView_Click(object sender, EventArgs e)
{
BindGridDetails();
}

let me know if you need more information.

Thanks




Responses

Posted by: Rajnilari2015 on: 3/15/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Santosh4u Sir,

it's not very clear from the statement "it's not working." . Is it giving any error?

After you have debugged, what is the response that you are getting..

Are u creating the UserControl dynamically? In that case u must load the usercontrol first.

In the meantime, you can turn off Event validation.

Having said all the above I am presenting some code snippet for you to go ahead.

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

</div>
<br />
<asp:Button ID="Button1" runat="server" Font-Bold="True" OnClick="Button1_Click"
Text="Export To Excel" />
and in code behind:-
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = BindData();
GridView1.DataBind();
}
}

private string ConnectionString
{
get { return @"Data Source=YASER\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"; }
}

private DataSet BindData()
{
// make the query
string query = "SELECT * FROM Products";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "Products");
return ds;
}


protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the
//specified ASP.NET server control at run time.
}


Please let us know about the progress.

--
Thanks & Regards,
RNA Team

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response