如何将GridView导出到Excel工作表?

时间:2011-09-09 10:49:49

标签: asp.net gridview export-to-excel

我的页面中有一个gridview。我想要的是当用户点击“导出”按钮时,它应该打开一个框来下载文件并保存为Excel工作表。我在网格中启用了pagging,但是当我导出数据时,所有行都必须在excel表中发送,而不管是否进行pagging。我无法导出我的数据源(datatable),因为它包含其他几个隐藏的列,但我只是为了我的目的而使用它,而不是向用户展示它。 我怎么能这样做...... ???我没有得到任何想法......

詹姆斯约翰逊说......我确实喜欢这样。这是他给出的代码

protected void btnExport_Click(object sender, EventArgs e)
{       
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";

    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new    System.Web.UI.HtmlTextWriter(oStringWriter);

    GridView1.RenderControl(oHtmlTextWriter);//Error is thrown from here.

    Response.Write(oStringWriter.ToString());
    Response.End();
}

但这样做会在代码

中的指定药水处引发跟踪错误
Control 'ctl00_ContentPlaceHolder1_ViewAdvances1_grdAdvance' of type 'GridView' must be  placed inside a form tag with runat=server.

我已将gridview放在usercontrol中,usercontrol放在.aspx页面中,该页面使用已有表单标记的母版页。

4 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

protected void btnExport_Click(object sender, EventArgs e)
{       
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";

    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

    GridView1.RenderControl(oHtmlTextWriter);

    Response.Write(oStringWriter.ToString());
    Response.End();
}

答案 1 :(得分:0)

关于此问题的讨论很多:

export gridview rows to excel sheet

Export GridView to Excel

Export gridview into excel in Windowsformsapplication(也许你会在这里找到一些有用的信息)

答案 2 :(得分:0)

将以下代码添加到您的代码中:

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    'dont delete - needed for excel export
End Sub

答案 3 :(得分:0)

我在母版页中获得了导出到excel的解决方案。

将此代码放在页面加载代码

之后
 public override void VerifyRenderingInServerForm(Control control)<br/>
    {<br/>
        /* Verifies that the control is rendered */<br/>
        //base.VerifyRenderingInServerForm(control);
    }

然后

protected void btnExportExcel_Click(object sender, EventArgs e)<br/>
    {<br/>
        try<br/>
        {<br/>
            BindReportdata(ddlReport.SelectedIndex);<br/>
            Response.Clear();<br/>
            Response.Buffer = true;<br/>
            Response.AddHeader("content-disposition",<br/>
             "attachment;filename=report.xls");<br/>
            Response.ContentType = "application/ms-excel";<br/>
            StringWriter sw = new StringWriter();<br/>
            HtmlTextWriter hw = new HtmlTextWriter(sw);<br/>
            for (int i = 0; i < gv_ReportData.Rows.Count; i++)<br/>
            {
                GridViewRow row = gv_ReportData.Rows[i];<br/>
            }
            gv_ReportData.GridLines = GridLines.Both;<br/>
            gv_ReportData.RenderControl(hw);<br/>
            Response.Write(sw.ToString());<br/>
            Response.Flush();<br/>
            Response.End();<br/>
        }<br/>
        catch (Exception ex)<br/>
        {<br/>
            throw ex;<br/>
        }<br/>
    }<br/>


并在aspx页面中使用updatepanel和触发按钮