出口的GridView到Excel#2 [英] export GridView to excel #2

查看:156
本文介绍了出口的GridView到Excel#2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的GridView绑定到一个SqlDataSource,然后_rowcreated事件做一些验证,当行不符合要求我使用隐藏它e.Row.Visible = FALSE;

这工作正常,并只在GridView显示正确的行。现在我有一个按钮,导出到Excel以及与出口也隐藏的行之外的伟大工程。我不想导出的隐藏的行。

有没有一种方法,我可以告诉GridView控件不添加该行,而不是隐藏它?
有一个简单的方法来删除所有隐藏的行我运行导出之前?
我不能出口过程中添加隐藏的行?正如你可以在code见下面我试着做这一个,但它不承认该行是否可见或不可见。

导出code:

 公共静态无效的出口(字符串文件名,GridView的GV)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader(
        内容处置的String.Format(附件;文件名= {0},文件名));
    HttpContext.Current.Response.ContentType =应用程序/ MS-Excel的;    使用(StringWriter的SW =新的StringWriter())
    {
        使用(HTW的HtmlTextWriter =新的HtmlTextWriter(SW))
        {
            //创建一个表单包含网格
            表的表=新表();            gv.GridLines = GridLines.Both;
            table.GridLines = gv.GridLines;
            //table.BackColor = Color.Yellow;            //标题行添加到表
            如果(gv.HeaderRow!= NULL)
            {
                GridViewExportUtil prepareControlForExport(gv.HeaderRow)。
                table.Rows.Add(gv.HeaderRow);                //颜色头
                table.Rows [0] .BackColor = gv.HeaderStyle.BackColor;
                table.Rows [0] .ForeColor = gv.HeaderStyle.ForeColor;
            }            //每个数据行添加到表
            的foreach(在gv.Rows GridViewRow行)
            {                如果(row.Visible ==真)
                {
                    GridViewExportUtil prepareControlForExport(行)。
                    table.Rows.Add(行);
                }
            }            //颜色的行
            布尔altColor = FALSE;
            的for(int i = 1; I< table.Rows.Count;我++)
            {
                如果(!altColor)
                {
                    table.Rows [I] .BackColor = gv.RowStyle.BackColor;
                    altColor = TRUE;
                }
                其他
                {
                    table.Rows [I] .BackColor = gv.AlternatingRowStyle.BackColor;
                    altColor = FALSE;
                }
            }            //渲染表到的HTMLWriter
            table.RenderControl(HTW);            //渲染的HTMLWriter到响应
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }
}


解决方案

在第一个地方,为什么你已经从你的GridView隐藏的行?只有得到你想要的数据。而使用这种方法导出;

 无效ExportToExcel(GridView控件grdData,字符串文件名)
{
    grdData.BorderStyle = BorderStyle.Solid;
    grdData.BorderWidth = 1;
    grdData.BackColor = Color.WhiteSmoke;
    grdData.GridLines = GridLines.Both;
    grdData.Font.Name =宋体;
    grdData.Font.Size = FontUnit.XXSmall;
    grdData.HeaderStyle.BackColor = Color.DimGray;
    grdData.HeaderStyle.ForeColor = Color.White;
    grdData.RowStyle.Horizo​​ntalAlign = Horizo​​ntalAlign.Left;
    grdData.RowStyle.VerticalAlign = VerticalAlign.Top;    HTT presponse响应= HttpContext.Current.Response;
    response.Clear();
    Response.Charset的=;
    response.ContentType =应用程序/ vnd.ms-EXCEL;
    response.AddHeader(内容处置,附件;文件名= \\+文件名+\\);    使用(VAR SW =新的StringWriter())
    {
        使用(VAR HTW =新的HtmlTextWriter(SW))
        {
            grdData.RenderControl(HTW);
            RESPONSE.WRITE(sw.ToString());
            到Response.End();
        }
    }
}

I am binding a GridView to a sqldatasource and then on _rowcreated event doing some validations and when the row does not meet requirements I am hiding it using e.Row.Visible = false;

This works fine and only displays the correct rows in the gridview. Now I have a button to export to excel and that works great with the exception of exporting also the hidden rows. I do not want the hidden rows exported.

Is there a way that i can tell the gridview to not add that row instead of hiding it? Is there a simple method to delete all hidden rows before i run the export? Can i not add the hidden rows during the export? As you can see in the code below I tried to do this one, but it does not recognize whether the row is visible or not.

Export Code:

 public static void Export(string fileName, GridView gv)
{
    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 form to contain the grid
            Table table = new Table();

            gv.GridLines = GridLines.Both;
            table.GridLines = gv.GridLines;
            //table.BackColor = Color.Yellow;



            //  add the header row to the table
            if (gv.HeaderRow != null)
            {
                GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                table.Rows.Add(gv.HeaderRow);

                //color the header
                table.Rows[0].BackColor = gv.HeaderStyle.BackColor;
                table.Rows[0].ForeColor = gv.HeaderStyle.ForeColor;
            }

            //  add each of the data rows to the table
            foreach (GridViewRow row in gv.Rows)
            {

                if (row.Visible == true)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }
            }

            //  color the rows
            bool altColor = false;
            for (int i = 1; i < table.Rows.Count; i++)
            {
                if (!altColor)
                {
                    table.Rows[i].BackColor = gv.RowStyle.BackColor;
                    altColor = true;
                }
                else
                {
                    table.Rows[i].BackColor = gv.AlternatingRowStyle.BackColor;
                    altColor = false;
                }
            }

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

解决方案

In the first place why you have hidden rows from your gridview? Only get data that you wanted. And use this method to export;

void ExportToExcel(GridView grdData, string filename)
{
    grdData.BorderStyle = BorderStyle.Solid;
    grdData.BorderWidth = 1;
    grdData.BackColor = Color.WhiteSmoke;
    grdData.GridLines = GridLines.Both;
    grdData.Font.Name = "Verdana";
    grdData.Font.Size = FontUnit.XXSmall;
    grdData.HeaderStyle.BackColor = Color.DimGray;
    grdData.HeaderStyle.ForeColor = Color.White;
    grdData.RowStyle.HorizontalAlign = HorizontalAlign.Left;
    grdData.RowStyle.VerticalAlign = VerticalAlign.Top;

    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.Charset = "";
    response.ContentType = "application/vnd.ms-excel";
    response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename+ "\"");

    using (var sw = new StringWriter())
    {
        using (var htw = new HtmlTextWriter(sw))
        {
            grdData.RenderControl(htw);
            response.Write(sw.ToString());
            response.End();
        }
    }
} 

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

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