嵌套的Gridview导出到Excel [英] Nested Gridview export to Excel

查看:116
本文介绍了嵌套的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

推荐答案

Praveen ..

使用后续功能只需传递要导出的网格视图ID,然后将名称命名为Excel Sheet作为后续功能的参数即可.

Hi,Praveen..

Use Following Function Just Pass Grid-view ID which is to export..... and name To Excel Sheet as Parameters to following Function..

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


       }


参考: http://www.eggheadcafe.com/community/visual-studio/7/10031792/eporting-nested-gridview.aspx [

这篇关于嵌套的Gridview导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆