EvanGWatkins
EvanGWatkins

Reputation: 1457

Code Upgrade - Export to Excel 2007 and up

I have some default functionality in a C# asp.net web application that will export a gridview to an excel file. This code is working great in Excel 2007, however it will not open in 2010. I need to upgrade this code to work in both, or find a new solution. Any help would be great.

Current code:

            gvReport.Style.Add("font-size", "1em");
            Response.Clear();
            string attachment = "attachment; filename=FileName.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gvReport.GridLines = GridLines.Horizontal;
            gvReport.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();

Upvotes: 0

Views: 4380

Answers (1)

Antonio Bakula
Antonio Bakula

Reputation: 20693

Use EPPlus and export data to Excel, I presume that gvReport iz GridView control, so use data that you bind to that GridView and export it with EPPlus.

It's pretty easy, here you can find example of ashx handler that will return excel file created from DataTable :

https://stackoverflow.com/a/9569827/351383

Upvotes: 2

Related Questions