将GridView数据导出到Excel的问题

编程入门 行业动态 更新时间:2024-10-10 19:23:10
本文介绍了将GridView数据导出到Excel的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将gridview数据导出到excel,但是当我打开该excel工作表时,它什么也没显示.我正在使用以下代码

I am trying to export gridview data to excel but when i open that excel sheet,it shows nothing. I am using the following code

protected void btnExport_Click(object sender, EventArgs e) { Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls")); Response.ContentType = "application/ms-excel"; System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gvDetails.AllowPaging = false; gvDetails.DataBind(); //Change the Header Row back to white color gvDetails.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Applying stlye to gridview header cells for (int i = 0; i < gvDetails.HeaderRow.Cells.Count; i++) { gvDetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1"); } int j = 1; //This loop is used to apply stlye to cells based on particular row foreach (GridViewRow gvrow in gvDetails.Rows) { if (j <= gvDetails.Rows.Count) { if (j % 2 != 0) { for (int k = 0; k < gvrow.Cells.Count; k++) { gvrow.Cells[k].Style.Add("background-color", "#EFF3FB"); } } } j++; } gvDetails.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } }

我还应用了服务器形式方法中的verify呈现方法,并将EnableEventValidation设置为false,但是在excel工作表中我没有任何数据. 请帮忙. gridview中的所有列都是模板字段.

I have also applied the verify rendering in server form method and have set the EnableEventValidation to false, but it in the excel sheet i did not get any data. Please help. All the columns in gridview are Template Fields.

推荐答案

尝试以下代码对我有用…… br/> Try Following Code It IS Working For Me............... public static void ExportToExcel(GridView gv, string filename) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename)); HttpContext.Current.Response.ContentType = "application/ms-excel"; using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // Create a table to contain the grid Table table = new Table(); // include the gridline settings table.GridLines = gv.GridLines; // add the header row to the table if (gv.HeaderRow != null) { PrepareControlForExport(gv.HeaderRow); table.Rows.Add(gv.HeaderRow); } //Make Header Coloruful for (int j = 0; j < gv.Columns.Count; j++) { //Apply style to Individual Cells gv.HeaderRow.Cells[j].Style.Add("background-color", "yellow"); } // add each of the data rows to the table foreach (GridViewRow row in gv.Rows) { PrepareControlForExport(row); table.Rows.Add(row); } // add the footer row to the table if (gv.FooterRow != null) { PrepareControlForExport(gv.FooterRow); table.Rows.Add(gv.FooterRow); } // render the table into the htmlwriter table.RenderControl(htw); // render the htmlwriter into the response HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } } }

要从Gridview导出数据........ 调用上方函数..as

To Export Data From Gridview........ Call Above Function..as

ExportToExcel(grd_data, "abc.xls")

更多推荐

将GridView数据导出到Excel的问题

本文发布于:2023-06-02 01:27:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/430952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:导出到   数据   GridView   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!