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

时间:2013-08-21 23:10:16

标签: c# asp.net excel gridview

我正在将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();

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

3 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

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

答案 1 :(得分:0)

使用此代码:这样可以正常工作..

aspx code ::

<%@ 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)
{

}

答案 2 :(得分:0)

解决了!!! 你的代码只有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();