如何从Griedview导出数据以简化代码 [英] How to export the data from Griedview to excel simple code

查看:74
本文介绍了如何从Griedview导出数据以简化代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在我的按钮点击事件上尝试了以下代码,但它显示错误,因为控件''GridView''类型'ctl00_ContentPlaceHolder1_grdsalarydetail''必须放在带有runat = server的表单标签内。 / b>我已尝试使用vb的相同逻辑代码我使用vb,在添加覆盖方法后可以使用





< pre lang =c#> Response.Clear();
Response.AddHeader( Content-disposition Attachmen; filename = reports.xlsx);
StringWriter StringWriter = new System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
grdsalarydetail.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();





但是任何人都可以在csharp中解释我如何使用与我相同的代码没有在csharp中获得正确的覆盖方法逻辑







 <   b  >  <   pre     lang   =  vb > ;  Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
'MyBase.VerifyRenderingInServerForm(control)
End Sub < / pre >
< / b >

解决方案

private void ExportGridToExcel(GridView grdGridView, string fileName)

{

Response.Clear();

Response.AddHeader(content-disposition,

string.Format(attachment; filename = {0} .xlsx,fileName));

Response.Charset =;

Response.ContentType = application / vnd.xlsx;



StringWriter stringWrite = new StringWriter();

HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);



grdGridView.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

响应。结束();

}

//你有覆盖此方法

public override void VerifyRenderingInServerForm(Control control)

{



}

< pre lang =   c#  >  


将Gridview数据导出到ASP.NET中的Excel [ ^ ]


实际上,有很多解决方案将数据从gridview导出到Excel。我担心人们可能无法成功地重现您的问题,为什么不尝试其他解决方案呢?它们易于操作,同时您可以从中学到很多东西。在这里我可以建议你一些,我相信它们对你有帮助,你想要简单的代码,这样你就可以自己决定哪个是最好的

9将数据导出到Excel for ASP.NET [ ^ ]

通过DataGridView将DataTable导出到Excel [ ^ ]

Excel to Datatable和Datatable to Excel [ ^ ]

Hi i have tried the following code on my button click event but it shows an error as Control ''ctl00_ContentPlaceHolder1_grdsalarydetail'' of type ''GridView'' must be placed inside a form tag with runat=server. i have tried the same logic of code i asp.net with vb which works after adding an overrides method as


Response.Clear();
        Response.AddHeader("Content-disposition", "Attachmen;filename=reports.xlsx");
        StringWriter StringWriter = new System.IO.StringWriter();
        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
        grdsalarydetail.RenderControl(HtmlTextWriter);
        Response.Write(StringWriter.ToString());
        Response.End();



but can any one explain in csharp how can i use the same code as i am not getting the correct logic of override method in csharp



<b><pre lang="vb">Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
        'MyBase.VerifyRenderingInServerForm(control)
    End Sub</pre>
</b>

解决方案

private void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.Clear();
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}.xlsx", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xlsx";

StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
//You have to override this method
public override void VerifyRenderingInServerForm(Control control)
{

}

<pre lang="c#">


Get a reference from Export Gridview Data to Excel in ASP.NET[^]


Actually, there are a lot of solutions to export data from gridview to Excel. I am afraid people may not successfully reproduce your problem, so why do not try other solutions? they are easy to perform and at the same time, you can learn much from them. Here I can suggest you some, I believe they are helpful for you, you want simple code, so you can decide yourself which is the best
9 Solutions to Export Data to Excel for ASP.NET[^]
Export DataTable to Excel through DataGridView[^]
Excel to Datatable and Datatable to Excel[^]


这篇关于如何从Griedview导出数据以简化代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆