1

I have a code that will generate an excel report.i am using ASP.NET MVC 4. I googled and found the same code everywhere still my code is not working why. My code in controller is as:

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");  
} 

Just to test the export functionality I used an array abc this code is working in .net application but not in ASP.NET MVC 4 application. I have debugged the code but not found a problem or error in code.

What am I doing wrong here?

2
  • If you put a breakpoint on the line after RenderControl, what is the content of sw? Commented Dec 4, 2013 at 9:46
  • i am getting {<div> <table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;"> <tr> <th scope="col">Item</th> </tr><tr> <td>AAA</td> </tr><tr> <td>BBB</td> </tr><tr> <td>CCC</td> </tr> </table> </div>} Commented Dec 4, 2013 at 10:03

3 Answers 3

3

Finally found a way to export to excel.

 public void ExportIniEmployeeToExcel(EligibilityCriteriaModel AllIniEmp)
    {
        List<AllIniEmployees> emp = new List<AllIniEmployees>();            
        AllIniEmp.allSuccessEmployeeList.ForEach(x =>
            {
                AllIniEmployees allEmp = new AllIniEmployees();
                allEmp.EmployeeCode = x.EmployeeCode;
                allEmp.EmployeeName = x.EmployeeName;
                allEmp.Designation = x.Designation;
                allEmp.DeliveryTeam = x.DeliveryTeam;
                allEmp.ConfirmationDate = x.ConfirmationDate;
                emp.Add(allEmp);
            });
        GridView gv = new GridView();
        gv.DataSource = emp;
        gv.DataBind();
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=InitiatedEmployees.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();
    }

here i used parent class as a parameter and used linq.now its working perfectly fine. thank you all.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't just write the output with a StringWriter. After your call to RenderControl, write:

byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
MemoryStream s = new MemoryStream(byteArray);
StreamReader sr = new StreamReader(s, Encoding.ASCII);

Response.Write(sr.ReadToEnd());
Response.End();

3 Comments

What is the problem now??
nothing happens there's no error while debugging.but the same code in asp.net C# app generates an excel.
Strange, it should work. Maybe instead of returning a RedirectToAction, you can just return some "dummy" JSON or a View.
0

Try returning a ContentResult instead of writing directly to the output stream.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.