Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have made an application in asp.net in which on click of gridview template field a javascript is called and from that javascript a new popup window is opened(not modal popup window), i have button in that popup window on click of which i have a function of exporting data to excel..everything works fine but the excel is not viewed as downloaded...

Please help here is my code..


C#
protected void btmExport_Click(object sender, EventArgs e)
    {
        //Response.Redirect("DownloadComplaintsExcel.aspx");
        DataSet ds = (DataSet)ViewState["ComplaintDataSet"];
        ExportDetails(ds.Tables[0]);
    }
 public void ExportDetails(DataTable dt)
    {
        string fileName = "Complaints-" + DateTime.Today.Day.ToString() + "" + DateTime.Today.Month.ToString() + "" + DateTime.Today.Year.ToString();
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\".xls");
       
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        DataGrid dg = new DataGrid();
        dg.DataSource = dt;
        dg.DataBind();
        dg.RenderControl(htw);
        response.Write(sw.ToString());
        response.End();
    }


Please help..

Regards,
Krunal
Posted

Hello,you can find something useful in below article:
9 Solutions to Export Data to Excel for ASP.NET[^]
 
Share this answer
 
Comments
[no name] 29-Sep-12 1:20am    
No solution is working.... Please help
Was not working in Popup so i have opened the page in new tab.. and started working..
 
Share this answer
 

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