繁体   English   中英

将GridView导出到Excel将导出整个页面

[英]Exporting GridView to Excel exports the whole page

我正在将GridView导出到Excel文件中,但是当我打开文件时,首先会收到关于格式类型和扩展名不匹配的错误,并且当我打开它时,整个页面都被带到Excel文件中,而不仅仅是网格视图。

我没有使用更新面板。 我尝试了GridView内部和外部的按钮,并得到了相同的结果,因此似乎是隐藏在代码后面的东西,看起来像这样:

  Response.Clear(); Response.Buffer = true; string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls"; Response.AddHeader("content-disposition", "attachment;filename=" + filename); Response.Charset = String.Empty; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView3.RenderControl(hw); //style to format numbers to string string style = @"<style> .textmode { mso-number-format:\\@; } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); 

您可以尝试以下方法:

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

使用此代码:可以正常工作。

aspx代码::

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Debug ="true" enableEventValidation ="false" Inherits="default"%>

这是aspx代码:

  protected void ConvertToExcel_Click(object sender, EventArgs e)     
  {
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Report.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();

    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

    for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
    {
        GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#bfc2c7");
    }
    int j = 1;

    foreach (GridViewRow gvrow in GridView1.Rows)
    {

        if (j <= GridView1.Rows.Count)
        {
            if (j % 2 != 0)
            {
                for (int k = 0; k < gvrow.Cells.Count; k++)
                {
                    gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                }
            }
        }
        j++;
    }
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();


}

public override void VerifyRenderingInServerForm(Control control)
{

}

解决了 !!! 您的代码是Response.Flush()的唯一正确问题; 在代码中使用而不是Response.Flush(); 您应该使用Response.End();

下面是工作代码:

        Response.Clear();

        Response.Buffer = true;
        string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls";
        Response.AddHeader("content-disposition",
        "attachment;filename=" + filename);
        Response.Charset = String.Empty;
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);


        GridView3.RenderControl(hw);
        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.End();
       // Response.Flush();
        HttpContext.Current.ApplicationInstance.CompleteRequest();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM