Eksportuj dane do pliku Excel za pomocą ASP.NET MVC 4 C # wyświetla się w widoku

Mam problem z eksportowaniem danych do Excela. Poniższe zdaje się renderować gridview do mojego widoku, zamiast monitować użytkownika o otwarcie programu Excel, który zainstalowałem na moim komputerze.

 Public ActionResult ExportToExcel()
{            
    var products = this.Repository.Products.ToList();

    var grid = new GridView();
    grid.DataSource = from p in products
                      select new
                      {
                          Id = p.Id,
                          Name = p.Name
                      };
    grid.DataBind();

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
    Response.ContentType = "application/ms-excel";

    Response.Charset = "";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    grid.RenderControl(htw);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();

    return View("MyView"); 
}

Co ja robię źle?

questionAnswers(6)

yourAnswerToTheQuestion