如何将数据从gridview传输到Excel文件 [英] how to transfer the data from a gridview to an excel file
本文介绍了如何将数据从gridview传输到Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
Interop是我发现处理的最简单方法通过C#精益求精.您可以在 ^ ].
两个选择:
互操作:
http://support.microsoft.com/kb/302084 [使用OLEDB读写Excel [ 受保护的 对象发件人,EventArgs e) { 此.EnableViewState= false ; // 如果发生错误,请将其与在源中设置的EnableEventValidation ="false"一起添加 Response.Clear(); Response.Buffer = true ; Response.AddHeader(" ," 附件; filename = exp.xls"); Response.Charset = " ; Response.ContentType = " ; StringWriter sw = 新 StringWriter(); HtmlTextWriter ht = 新 HtmlTextWriter(sw); GridView1.AllowPaging = false ; GridView1.DataBind(); GridView1.RenderControl(ht); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } 公共 覆盖 void VerifyRenderingInServerForm(System.Web .UI.Control控件) { }
hi i wanted the code for transferring the grid view data to an excel file.on button click what action has to be performed?
解决方案
Interop is easiest way I found dealing with excel through C#. You will find many articles on this[^].
Two choises:
Interop:
http://support.microsoft.com/kb/302084[^]
OleDb:
Reading and Writing Excel using OLEDB[^]
Other options could be found on Google.
protected void Button1_Click(object sender, EventArgs e) { this.EnableViewState = false; //in case error occurs add this along with EnableEventValidation="false" set in source Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=exp.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter ht = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); GridView1.RenderControl(ht); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } public override void VerifyRenderingInServerForm(System.Web.UI.Control control) { }
这篇关于如何将数据从gridview传输到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文