将gridview导出为带有标题和摘要的Excel [英] Export gridview to excel with heading and summary

查看:139
本文介绍了将gridview导出为带有标题和摘要的Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我将gridview导出为ex​​cel。但是我希望在第一排的excel表格和夏季的时候走下去。我使用下面的代码。



 public static void Export(string fileName,GridView gv)
{
string style = @< style> .text {mso-number-format:\ @;}< / style>;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(content-disposition,string.Format(attachment; filename = {0},fileName));
HttpContext.Current.Response.ContentType =application / ms-excel;
DataTable dt = new DataTable();

using(StringWriter sw = new StringWriter())
{
using(HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create包含网格的表单
表table = new Table();

//将标题行添加到表中

if(gv.HeaderRow!= null)
{
PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
table.Rows [0] .BackColor = Color.Yellow;
}

//将每个数据行添加到表
foreach(gv.Rows中的GridViewRow行)
{
PrepareControlForExport(row) ;
table.Rows.Add(row);

}

//将页脚行添加到表
if(gv.FooterRow!= null)
{
PrepareControlForExport( gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
table.GridLines = GridLines.Both;

//将表格渲染到htmlwriter
table.RenderControl(htw);

HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}

private static void PrepareControlForExport(控制控制)
{
for(int i = 0; i< control.Controls.Count; i ++)
{
Control current = control.Controls [i];
if(current is Label)
{
control.Controls.Remove(current);
control.Controls.AddAt(i,new LiteralControl((current as Label).Text));
}
if(current is LinkBut​​ton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i,new LiteralControl((当前为LinkBut​​ton).Text));
}
else if(current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i,new LiteralControl((current as ImageButton).AlternateText));
}
else if(current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i,new LiteralControl((当前为HyperLink).Text));
}
else if(current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i,new LiteralControl((当前为DropDownList).SelectedItem.Text));
}
else if(current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i,new LiteralControl((当前为CheckBox).Checked?True:False));
}

if(current.HasControls())
{
PrepareControlForExport(current);
}
}
}

解决方案



本文导出带有标题的GridView:



http://www.aspdotnet-suresh.com/2011/12/export-selected-gridview-rows-to-excel.html [<一个href =http://www.aspdotnet-suresh.com/2011/12/export-selected-gridview-rows-to-excel.html\"target =_ blanktitle =新窗口> ^ ]

Hi,

I am exporting gridview to excel. But I want heading in first row of excel sheet and summery at down. I have used below code.

public static void Export(string fileName, GridView gv)
{
    string style = @"<style> .text { mso-number-format:\@; } </style> ";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    DataTable dt = new DataTable();

    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            // Create a form to contain the grid
            Table table = new Table();

            // add the header row to the table

            if (gv.HeaderRow != null)
            {
                PrepareControlForExport(gv.HeaderRow);
                table.Rows.Add(gv.HeaderRow);
                table.Rows[0].BackColor = 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);
            }
            table.GridLines = GridLines.Both;

            // render the table into the htmlwriter
            table.RenderControl(htw);

            HttpContext.Current.Response.Write(style);
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }
}

private static void PrepareControlForExport(Control control)
{
    for (int i = 0; i < control.Controls.Count; i++)
    {
        Control current = control.Controls[i];
        if (current is Label)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as Label).Text));
        }
        if (current is LinkButton)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
        }
        else if (current is ImageButton)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
        }
        else if (current is HyperLink)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
        }
        else if (current is DropDownList)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
        }
        else if (current is CheckBox)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
        }

        if (current.HasControls())
        {
            PrepareControlForExport(current);
        }
    }
}

解决方案

Hi,
This article exports a GridView with its header:

http://www.aspdotnet-suresh.com/2011/12/export-selected-gridview-rows-to-excel.html[^]


这篇关于将gridview导出为带有标题和摘要的Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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