【问题标题】:Exporting GridView to Excel exports only <div> and </div>将 GridView 导出到 Excel 仅导出 <div> 和 </div>
【发布时间】:2018-08-23 14:58:34
【问题描述】:

我是 .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 功能。 在导出函数之后放置这个函数是行不通的。它根本不允许我导出。

我可能做错了什么?

【问题讨论】:

  • “我在导出的文档中得到标签”是什么意思?究竟是什么错误或问题?
  • 你做错的事情是你不是导出一个excel文件,而是一个带有xlsx扩展名的html页面。使用像 EPPlus 这样的专用库。
  • 我认为不需要任何外部库。我相信数据没有正确绑定。这是图片链接i.stack.imgur.com/iYCEQ.png

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


【解决方案1】:

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

检查示例代码。

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多