本文介绍了嵌套的Gridview导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要将嵌套的网格导出为ex​​cel格式.

任何有想法或代码的人.

帮助pl

问候
Praveen

Hello all,

I need to export the nested grid to excel format.

anybody having an idea or code.

Help pl

Regards
Praveen

推荐答案

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导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:01