将 GridView 导出到 Excel 仅导出 <div> 和 </div>

Exporting GridView to Excel exports only <div> and </div>

我是 .net 的新手。我写了一个代码将 gridview 导出到 excel。导出有效,但我没有获取 gridview 的数据,而是在导出的文档中获取标签。如果我将 gridview 导出到文档,情况相同。这是导出文件的样子

我的 GridView 由搜索查询填充。这是我的代码的摘录:

Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    MsgBox("Exporting")

    Response.Clear()
    Response.Buffer = True

    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"

    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)

    gridviewID.AllowPaging = False
    gridviewID.DataSource = gvDataTable
    gridviewID.DataBind()
    gridviewID.RenderControl(hw)

    Response.Write(sw.ToString())
    Response.Flush()
    Response.End()

我的 GridView 不在 Form 标签内,因为我的母版页中已有 Form 标签。这是我的 GridView 的样子:

<asp:GridView ID="gridviewID" runat="server"
                        AutoGenerateColumns="False" onrowcommand="searchResultsGridView_RowCommand">
                        <Columns>
                            <asp:BoundField DataField="logNum" HeaderText="ID" ReadOnly="True" ItemStyle-CssClass="hideme" />

                            <asp:BoundField DataField="workIDName" HeaderText="work NAME"
                              SortExpression="workIDName" ReadOnly="True" />

                            <asp:BoundField DataField="LeadIdName" HeaderText=" LEAD"
                              SortExpression="LeadIdName" ReadOnly="True" />

                            <asp:BoundField DataField="Lead_Name2"// and a lot of fields till closing tags

我在页面加载后立即放置了 verifyrendering 函数。 将此函数放在导出函数之后是行不通的。它根本不让我导出。

我可能做错了什么?

您似乎在将数据绑定到网格视图之前执行了导出功能。尝试在单独的事件中加载数据并在 Button4_Click 事件中执行导出功能。

检查示例代码。

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 Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = BindData();
            GridView1.DataBind(); 
        }
    }

    private string ConnectionString
    {
        get { return @"Server=localhost;Database=Northwind;
        Trusted_Connection=true"; }
    }

    private DataSet BindData()
    {
        // make the query 
        string query = "SELECT * FROM Categories";
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds, "Categories");
        return ds;
    }
    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();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for the
        specified ASP.NET server control at run time.
    }

//resource : https://www.daniweb.com/programming/web-development/threads/360606/how-to-export-data-from-gridview-to-excel-in-asp-net
}