简体   繁体   中英

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM