1

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

4

3 回答 3

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();
        }
    }
}
于 2013-08-22T05:02:08.520 回答
0

使用此代码:这将正常工作..

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

}
于 2013-08-23T05:06:42.403 回答
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();
于 2016-05-10T11:06:13.573 回答