Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将GridView导出到excel#2_C#_Asp.net_Excel_Gridview_Export - Fatal编程技术网

C# 将GridView导出到excel#2

C# 将GridView导出到excel#2,c#,asp.net,excel,gridview,export,C#,Asp.net,Excel,Gridview,Export,我将GridView绑定到sqldatasource,然后在_rowcreated事件上执行一些验证,当该行不满足要求时,我使用e.row.Visible=false隐藏它 这很好,只在gridview中显示正确的行。现在我有一个按钮可以导出到excel,除了导出隐藏的行外,效果非常好。我不希望导出隐藏行 有没有一种方法可以告诉gridview不要添加该行而不是隐藏它? 是否有一种简单的方法可以在运行导出之前删除所有隐藏行? 在导出过程中是否可以不添加隐藏行?正如您在下面的代码中看到的,我尝试过

我将GridView绑定到sqldatasource,然后在_rowcreated事件上执行一些验证,当该行不满足要求时,我使用e.row.Visible=false隐藏它

这很好,只在gridview中显示正确的行。现在我有一个按钮可以导出到excel,除了导出隐藏的行外,效果非常好。我不希望导出隐藏行

有没有一种方法可以告诉gridview不要添加该行而不是隐藏它? 是否有一种简单的方法可以在运行导出之前删除所有隐藏行? 在导出过程中是否可以不添加隐藏行?正如您在下面的代码中看到的,我尝试过这样做,但它无法识别行是否可见

出口代码:

 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();
        }
    }
}
publicstaticvoid导出(字符串文件名,GridView-gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
“内容处置”,string.Format(“附件;文件名={0}”,文件名));
HttpContext.Current.Response.ContentType=“应用程序/ms excel”;
使用(StringWriter sw=new StringWriter())
{
使用(HtmlTextWriter htw=新的HtmlTextWriter(sw))
{
//创建一个表单以包含网格
Table Table=新表();
gv.GridLines=网格线。两者都是;
table.GridLines=gv.GridLines;
//table.BackColor=Color.Yellow;
//将标题行添加到表中
如果(gv.HeaderRow!=null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
表.行.添加(gv.HeaderRow);
//给标题上色
table.Rows[0]。BackColor=gv.HeaderStyle.BackColor;
table.Rows[0]。前景色=gv.HeaderStyle.ForeColor;
}
//将每个数据行添加到表中
foreach(gv.Rows中的GridViewRow行)
{
if(row.Visible==true)
{
GridViewExportUtil.PrepareControlForExport(行);
table.Rows.Add(行);
}
}
//给行上色
bool-altColor=false;
对于(int i=1;i

我从未使用过GridViewExportUtil,但为什么不编辑他的代码呢

private static void PrepareControlForExport(Control control)
{
    for (int i = 0; i < control.Controls.Count; i++)
    {
        Control current = control.Controls[i];

        //-----------------------------
        // * my addition
        if (!current.Visible) continue;
        //-----------------------------

        if (current is LinkButton)
        {
            control.Controls.Remove(current);
            control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
        }
        else if (current is ImageButton)
        {
            //...
private static void PrepareControlForExport(控件)
{
对于(int i=0;i
以下是将GridView导出到Excel的简单实现:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; 

public partial class ExportGridView : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();

        Response.AddHeader("content-disposition", "attachment;
        filename=FileName.xls");

        Response.Charset = "";

        // If you want the option to open the Excel file without saving than

        // comment out the line below

        // Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite =
        new HtmlTextWriter(stringWrite);

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End();

    }
}

首先,为什么要从gridview中隐藏行?只获取所需的数据。然后使用此方法导出

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

为什么首先要隐藏行?只获取要显示的数据。几乎所有答案都在此处写入(HtmlTextWriter)字符串或互操作代码。不要使用任何一种。这将导致以后在日期时间和十进制格式方面出现问题。此外,Excel将发出警告,因为您没有生成“real”Excel文件,但是扩展名为.xls的HTML页面。开始使用专门的库来创建Excel文件,如.and。