Click here to Skip to main content
14,665,547 members
Articles » Web Development » ASP.NET » Howto
Posted 5 Feb 2012

Tagged as


41 bookmarked

MVC Grid to Excel file download

Rate this:
4.43 (15 votes)
Please Sign up or sign in to vote.
4.43 (15 votes)
8 Feb 2012CPOL
Convert the results of a ASP.NET MVC grid to a downloadable Excel file


This article is a "how to" on converting a tabulated or grid representation of data from an ASP.NET MVC web application to a downloadable Excel data file.

Image 1

Image 2

Image 3


I was recently building an ASP.NET MVC 3 based reporting application. One of the features was to provide a downloadable Excel file for the report that was being rendered as a grid/table. Rather than recreate an entire module to accomplish this, I wanted to find a way to reuse the data being used to render the view, with the least amount of code possible.

Using the code

I have tried to encapsulate much of the implementation in a custom ActionResult class (DownloadFileActionResult), so you can just pass data in the form of a GridView object and expect a downloadable Excel file with tabulated data.

You can download the complete code from the below link.

As stated above most of the implementation is contained in the DownloadFileActionResult class.

Firstly the DownloadFileActionResult inherits from the abstract ActionResult class, the constructor accepts two parameters the databound gridview object and the name of the downloadable Excel file.

The ExecuteResult is overridden with the custom implementation that creates a response, then uses a HtmlTextWriter and a StringWriter to pipe the rendering of a gridview to a string. This string is then streamed back to the response to be delivered as an Excel file. Its that simple

public class DownloadFileActionResult : ActionResult

        public GridView ExcelGridView { get; set; }
        public string fileName { get; set; }

        public DownloadFileActionResult(GridView gv, string pFileName)
            ExcelGridView = gv;
            fileName = pFileName;

        public override void ExecuteResult(ControllerContext context)

           //Create a response stream to create and write the Excel file
            HttpContext curContext = HttpContext.Current;
            curContext.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            curContext.Response.Charset = "";
            curContext.Response.ContentType = "application/";

            //Convert the rendering of the gridview to a string representation 
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            //Open a memory stream that you can use to write back to the response
            byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
            MemoryStream s = new MemoryStream(byteArray);
            StreamReader sr = new StreamReader(s, Encoding.ASCII);

            //Write the stream back to the response



Within your controller's Action result where you bind the view with model data you will need to create a gridview, bind the model data to it and store it in a session variable.

public ActionResult Index()
           ViewBag.Message = "Welcome to ASP.NET MVC!";
           CarModels cm = new CarModels();
           List<Cars> model = cm.getAllCars();

           GridView gv = new GridView();
           gv.DataSource = model;
           Session["Cars"] = gv;

           return View(model);

In the same controller we need to create a new ActionResult called Download to handle the file download request.

public ActionResult Download()
            if (Session["Cars"] != null)
                return new DownloadFileActionResult((GridView)Session["Cars"], "Cars.xls");
               //Some kind of a result that will indicate that the view has 
               //not been created yet. I would use a Javascript message to do so. 

In the view you place the following download link

@Html.ActionLink("Download File", "Download")

Points of Interest

I hope this article goes to show how you can override the default ActionResult class with a custom implementation to generate a custom response.

I am somehow not too happy about storing the results of the grid in a Session variable and will endeavor to come up with a for a better solution.


As this is html being rendered as an Excel sheet you will not be able to make use of most of the Excel functionality. In my case, the client wanted was a downloadable version of the data so it was fine. But in case you want to create a true Excel file you could look at using XSLT to convert your html output to an Excel file.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ritesh Ramesh
Architect Infosolvex Solutions Inc
Australia Australia
Ritesh is an IT consultant with over ten years of experience in the IT industry varying from consultation, architecture, design, development to technical management. He has a strong background in solutions and applications architecture with a focus on Microsoft’s .Net platform. His area of expertise spans design and implementation of client/server, database and web-based systems. He has worked with C#, ASP.NET 1.1 and 2.0, ADO.NET, Web Services and SQL technology on several enterprise class projects.

Freedom is not worth having if it does not include the freedom to make mistakes.
Mahatma Gandhi

Comments and Discussions

QuestionGridView issues Pin
Member 1210021420-May-19 5:56
MemberMember 1210021420-May-19 5:56 
QuestionDo you have a better way to do it without storing in session variables? Pin
phatlee25-May-18 7:19
Memberphatlee25-May-18 7:19 
Questionhow to export filtered grid Pin
Member 133125107-Aug-17 2:34
MemberMember 133125107-Aug-17 2:34 
QuestionExport Filtered Data Pin
Member 1212068916-Nov-15 10:58
MemberMember 1212068916-Nov-15 10:58 
GeneralThanks! Pin
steven717-Nov-14 23:41
Membersteven717-Nov-14 23:41 
QuestionHow do I generate multiple Sheets Pin
sumitp1715-May-14 20:40
Membersumitp1715-May-14 20:40 
QuestionIt solved Pin
Nanda Rengasamy24-Sep-13 2:25
MemberNanda Rengasamy24-Sep-13 2:25 
GeneralMy vote of 1 Pin
Jaydeep Jadav8-Mar-13 22:38
MemberJaydeep Jadav8-Mar-13 22:38 
Questioncode is working in google chrome browser but not in ie Pin
magy11-Feb-13 19:43
Membermagy11-Feb-13 19:43 
QuestionLog Kuchh bhi dalate hai.. Pin
Member 862979031-Oct-12 6:10
MemberMember 862979031-Oct-12 6:10 
GeneralMy vote of 4 Pin
andy(-RKO)1-May-12 4:13
Memberandy(-RKO)1-May-12 4:13 
GeneralMy vote of 3 Pin
xExTxCx7-Feb-12 7:37
MemberxExTxCx7-Feb-12 7:37 
QuestionNot real XLS file Pin
Robert Hutch5-Feb-12 22:03
MemberRobert Hutch5-Feb-12 22:03 
Your solution is very intuitive and convenient for users that view their reports with only Microsoft Excel.

But if they would also want to do additional processing on XLS file with some library or they don't have MS Office installed, they are stuck because you are exporting HTML, not real binary Excel file, in XLS format.

If you are not exporting any formatting or other Excel specific properties, just plain tabular data, then export it as CSV, or otherwise use some Excel C# / VB.NET library to export to real Excel files - binary XLS or zipped xml - XLSX.
AnswerRe: Not real XLS file Pin
Ritesh Ramesh8-Feb-12 7:35
MemberRitesh Ramesh8-Feb-12 7:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.