Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a method in controller which should generate excel.my code in controller is as:
C#
public ActionResult ExportData()
{
string[] abc = { "AAA", "BBB", "CCC" };
            GridView gv = new GridView();
            gv.DataSource = abc;
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=Marklist.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            return RedirectToAction("_EligibilityCriteria");    
}

i googled and found this is the only code.this code is working in .net application but not in mvc.kindly reply what i am doing wrong in code.
Posted
Comments
thatraja 4-Dec-13 5:25am    
not working? what's the error, include those details always in your question
sid2586 4-Dec-13 5:43am    
the above same code when executed in Asp.net website generates an excel sheet.but in mvc nothing is happening on debugging also there is no error in code.
sid2586 9-Dec-13 8:53am    
i found something new in this while debugging when the ExportData()method is executed control goes to jquery file.while in .net the control ends in the method itself.any idea how to stop the control going back to jquery.

1 solution

Have you tried to use Google[^]?
Sample links:
MVC3 Export To Excel[^]
Export to Excel in Asp.net MVC [closed][^]
MVC: Exporting data to .XLS “Excel Files”[^]

Next time, please be more specific. Before you ask a question, please try to debug an application.
 
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