如何在VB.NET中实现把SQL数据库里表的数据导入到Excel中?

 我来答
CA00018
2014-09-02 · 超过68用户采纳过TA的回答
知道答主
回答量:118
采纳率:0%
帮助的人:126万
展开全部
asp.net 中将gridview数据导入到excel的类 usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.IO; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI. WebControls; usingSystem.Web.UI. WebControls.WebParts; usingSystem.Web.UI. HtmlControls; ///<summary> /// ///</summary> publicclass GridViewExportUtil { ///<summary> /// ///</summary> ///<paramname="fileName" ></param> ///<paramname="gv"></ param> publicstaticvoidExport( stringfileName,GridViewgv) { HttpContext.Current. Response.Clear(); HttpContext.Current. Response.AddHeader( "content- disposition",string.Format(" attachment;filename={0}", System.Web.HttpUtility. UrlEncode(fileName,System. Text.Encoding.UTF8))); HttpContext.Current. Response.ContentType=" application/ms-excel"; HttpContext.Current. Response.Charset="utf-8"; HttpContext.Current. Response.ContentEncoding= System.Text.Encoding. GetEncoding("utf-8"); using(StringWriter sw=newStringWriter()) { using( HtmlTextWriterhtw=new HtmlTextWriter(sw)) { //Createa formtocontainthegrid Tabletable= newTable(); //addthe headerrowtothetable if(gv. HeaderRow!=null) { GridViewExportUtil. PrepareControlForExport(gv. HeaderRow); table. Rows.Add(gv.HeaderRow); } //addeach ofthedatarowstothetable foreach( GridViewRowrowingv.Rows) { GridViewExportUtil. PrepareControlForExport(row); table. Rows.Add(row); } //addthe footerrowtothetable if(gv. FooterRow!=null) { GridViewExportUtil. PrepareControlForExport(gv. FooterRow); table. Rows.Add(gv.FooterRow); } //render thetableintothehtmlwriter table. RenderControl(htw); //render thehtmlwriterintothe response HttpContext. Current.Response.Write(sw. ToString()); HttpContext. Current.Response.End(); } } } 作者: 后天美丽 2007-8-31 09:24 回复此发言 2 回复: asp.net 中将gridview数据导入到excel 的类 ///<summary> ///Replaceanyofthe containedcontrolswith literals ///</summary> ///<paramname="control"></ param> privatestaticvoid PrepareControlForExport( Controlcontrol) { for(inti=0;i<control. Controls.Count;i++) { Controlcurrent=control. Controls[i]; if(currentisLinkButton) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asLinkButton).Text)); } elseif(currentis ImageButton) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asImageButton).AlternateText) ); } elseif(currentis HyperLink) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asHyperLink).Text)); } elseif(currentis DropDownList) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asDropDownList).SelectedItem. Text)); } elseif(currentisCheckBox) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asCheckBox).Checked?"True" :"False")); } if(current.HasControls()) { GridViewExportUtil. PrepareControlForExport( current); } } } }
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式