如何使用 C# lang 将 gridview 数据转换为 ASP .net 中的 Excel 工作表

发布于 2024-11-18 16:40:16 字数 827 浏览 2 评论 0原文

我想将 GridView 数据转换为 Excel 工作表。

我已经编写了下面的代码,但它给出了错误:

protected void Button1_Click(object sender, EventArgs e) 
{       
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Avukat.xls");
    Response.Charset = "";

    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("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
    Response.Write(stringWrite.ToString());
    Response.End();
}

错误:

“GridView”类型的控件“ctl00_ContentPlaceHolder1_GridView1”必须放置在带有 runat=server 的表单标记内。

I want to covnvert GridView data to Excel sheet.

I have written the code below, but it gives error:

protected void Button1_Click(object sender, EventArgs e) 
{       
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Avukat.xls");
    Response.Charset = "";

    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("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
    Response.Write(stringWrite.ToString());
    Response.End();
}

Error:

Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

番薯 2024-11-25 16:40:16

我认为您的 gridview 包含 linkbutton/Imagebutton 或其他类型的控件,这就是当您尝试将 GridView 导出到 Excel 时出现异常的原因。

在使用该控件之前,您需要在 Page 代码后面或 BasePage 代码后面添加以下几行。

public override void VerifyRenderingInServerForm(Control control)
{
}

您可以使用此代码,因为此代码经过测试并且运行良好:

System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.AddHeader("content-disposition", "attachment; filename=Avukat.xls");
Response.ClearContent();

Response.AddHeader("content-disposition", attachment);

GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();

I think your gridview contains a linkbutton/Imagebutton or another type of control, and that's why you are getting an Exception when you are trying to export the GridView to Excel.

Before using the control you need to add the following lines in your Page code behind, or the BasePage code behind.

public override void VerifyRenderingInServerForm(Control control)
{
}

You can use this code, as this code is tested and worked perfectly:

System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.AddHeader("content-disposition", "attachment; filename=Avukat.xls");
Response.ClearContent();

Response.AddHeader("content-disposition", attachment);

GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
ˇ宁静的妩媚 2024-11-25 16:40:16

你错过了这一部分:

HtmlForm _HtmlForm= new HtmlForm();
GridView1.Parent.Controls.Add(_HtmlForm);
_HtmlForm.Attributes["runat"] = "server";
_HtmlForm.Controls.Add(GridView1);
_HtmlForm.RenderControl(htmlWrite);

我的意思是你应该使用这行代码:

_HtmlForm.RenderControl(htmlWrite);

而不是:

GridView1.RenderControl(htmlWrite); 

但我认为最好使用这个免费的开源组件,它可以从/导出不同的 Excel 和 Word 格式: http://npoi.codeplex.com/

You've missed this part:

HtmlForm _HtmlForm= new HtmlForm();
GridView1.Parent.Controls.Add(_HtmlForm);
_HtmlForm.Attributes["runat"] = "server";
_HtmlForm.Controls.Add(GridView1);
_HtmlForm.RenderControl(htmlWrite);

I mean you should use this line of code:

_HtmlForm.RenderControl(htmlWrite);

Instead of:

GridView1.RenderControl(htmlWrite); 

But I think it would be better to use this free open source component which imports/exports from/to different excel and word formats: http://npoi.codeplex.com/

幸福还没到 2024-11-25 16:40:16

您只需要复制下面的代码。

public override void VerifyRenderingInServerForm(Control control)
    {

        // Confirms that an HtmlForm control is rendered for the
        specified ASP.NET server control at run time.

    }

You just need to copy below code.

public override void VerifyRenderingInServerForm(Control control)
    {

        // Confirms that an HtmlForm control is rendered for the
        specified ASP.NET server control at run time.

    }
迎风吟唱 2024-11-25 16:40:16
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

this.gvTask.RenderBeginTag(htmlWrite);
this.gvTask.HeaderRow.RenderControl(htmlWrite);
foreach (GridViewRow row in this.gvTask.Rows)
{
    row.RenderControl(htmlWrite);
}
this.gvTask.FooterRow.RenderControl(htmlWrite);
this.gvTask.RenderEndTag(htmlWrite); 
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

this.gvTask.RenderBeginTag(htmlWrite);
this.gvTask.HeaderRow.RenderControl(htmlWrite);
foreach (GridViewRow row in this.gvTask.Rows)
{
    row.RenderControl(htmlWrite);
}
this.gvTask.FooterRow.RenderControl(htmlWrite);
this.gvTask.RenderEndTag(htmlWrite); 
魂牵梦绕锁你心扉 2024-11-25 16:40:16
//**how gridview data download in excel in asp.net**

protected void btnDownload_Click(object sender, EventArgs e)
{
    CommonFunction objComm = new CommonFunction();
    Hashtable objHash = new Hashtable();
    //Hashtable htParam = new Hashtable();
    objHash.Clear();
    string str = ddlMonthlyYrs.SelectedItem.Text.ToString();
    if (str == "Select")
    {
        objHash.Add("@Cmonth", "");
    }
    else
    {
        objHash.Add("@Cmonth", str.Substring(0, 6));
    }
    if (ddlPaymentTerm.SelectedItem.Text == "Select")
    {
        objHash.Add("@PaymentTerm", "");
    }
    else
    {
        objHash.Add("@PaymentTerm", ddlPaymentTerm.SelectedValue.ToString());
    }
    if (ddlPayMode.SelectedItem.Text == "Select")
    {
        objHash.Add("@PaymentMode", "");
    }
    else
    {
        objHash.Add("@PaymentMode", ddlPayMode.SelectedValue);
    }

    objHash.Add("@PolicyNo", txtPolicyNo.Text);

    objHash.Add("@AgentCode", txtAgnCode.Text);
    objHash.Add("@AgentName", txtAgnName.Text);

    DataSet objDS = objComm.GetDataSetForPrcDBConn("Prc_GetIncBasedataRp", objHash, "Commssion");

    BasicComm.CommonFunction objCom = new BasicComm.CommonFunction();
    Response.Clear();
    //Response.Charset=”";
    Response.ContentType = "application/vnd.ms-excel";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
    System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
    dg.DataSource = objDS.Tables[0];
    dg.DataBind();
    dg.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}
//**how gridview data download in excel in asp.net**

protected void btnDownload_Click(object sender, EventArgs e)
{
    CommonFunction objComm = new CommonFunction();
    Hashtable objHash = new Hashtable();
    //Hashtable htParam = new Hashtable();
    objHash.Clear();
    string str = ddlMonthlyYrs.SelectedItem.Text.ToString();
    if (str == "Select")
    {
        objHash.Add("@Cmonth", "");
    }
    else
    {
        objHash.Add("@Cmonth", str.Substring(0, 6));
    }
    if (ddlPaymentTerm.SelectedItem.Text == "Select")
    {
        objHash.Add("@PaymentTerm", "");
    }
    else
    {
        objHash.Add("@PaymentTerm", ddlPaymentTerm.SelectedValue.ToString());
    }
    if (ddlPayMode.SelectedItem.Text == "Select")
    {
        objHash.Add("@PaymentMode", "");
    }
    else
    {
        objHash.Add("@PaymentMode", ddlPayMode.SelectedValue);
    }

    objHash.Add("@PolicyNo", txtPolicyNo.Text);

    objHash.Add("@AgentCode", txtAgnCode.Text);
    objHash.Add("@AgentName", txtAgnName.Text);

    DataSet objDS = objComm.GetDataSetForPrcDBConn("Prc_GetIncBasedataRp", objHash, "Commssion");

    BasicComm.CommonFunction objCom = new BasicComm.CommonFunction();
    Response.Clear();
    //Response.Charset=”";
    Response.ContentType = "application/vnd.ms-excel";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
    System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
    dg.DataSource = objDS.Tables[0];
    dg.DataBind();
    dg.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文