将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")