1、首先打开Visual Studio 2008代码窗口,添加引用。
2、输入以下代码:Public conn1 As SqlConnection = New SqlConnection 。
3、声明关键字 Public;(因为是全局变量,所以用Public 来声明)。
4、如果SQL 数据库就在本机,则用以下代码连接。
5、如果代码太长,影响可读性,可以用空格加"_"后,回车换行即可。
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(" attachmentfilename={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=0i<control. Controls.Counti++) { 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) } } } }欢迎分享,转载请注明来源:内存溢出
评论列表(0条)