如何在VB.NET中实现把SQL数据库里表的数据导入到Excel中?
展开全部
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); } } } }
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询