在ASP.NET中將資料直接輸出成Excel內容 (轉)

gugu99發表於2007-12-05
在ASP.NET中將資料直接輸出成Excel內容 (轉)[@more@]

在中將資料直接輸出成內容

 

本文實現了將中的資料直接輸出到Excel格式並在裡輸出。下面就是實現的例子:

playground.com/Exam/Data2ExcelDemo.htm" target=_target>檢視例子

ExcelExport.

.mengxianhui.com.ExcelExport&quot;%&gt; w3c//DTD HTML 4.0 Transitional//EN&quot;&gt; <title>ExcelExport</title> <meta name="GENERATOR" content="a href=" tag-126-1.html="">microsoft .NET 7.0&quot;&gt; <meta name="CODE_LANGUAGE" content="a href=" tag-50-1.html=""> 7.0&quot;&gt; <meta name="vs_defaultClientScript" content="a href=" tag-133-1.html="">script&quot;&gt; <meta name="vs_targetSchema" content=" href=" tag-340-1.html="">intellisense/&quot;&gt; <fo href="http://blog.itpub.net/10748419/viewspace-988555/tag-415-1.html">RM id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;&gt; edItemStyle Font-Bold=&quot;True&quot; ForeColor=&quot;#663399&quot; BackColor=&quot;#FFCC66&quot;&gt; 是 Web 窗體設計器所必需的。 <system href="http://blog.itpub.net/10748419/viewspace-988555/tag-268-1.html">buggerStepThrough()&gt; Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System., ByVal e As System.EventArgs) _ Handles MyBase.Init 'CODEGEN: 此方法呼叫是 Web 窗體設計器所必需的 '不要使用程式碼編輯器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load '在此處放置初始化頁的程式碼 ' 定義是否是 SERVER 資料庫,這裡為False Dim blnIsSQLServer As System.Boolean = False Dim strSQL As String Dim objDataset As New DataSet() Dim objConn As Object Dim strCnn As String If blnIsSQLServer Then strCnn = &quot;User ID=sa;Initial Catalog=Northwind;Data =.NetSDK;&quot; objConn = New System.Data.SqlClient.SqlConnection(strCnn) objConn.Open() Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter() strSQL = &quot;Select * from customers where country='USA'&quot; objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn) objAdapter.Fill(objDataset) Else strCnn = &quot;Provr=Microsoft.Jet.OLE.4.0;Data Source=&quot; + Server.MapPath(&quot;Test.mdb&quot;) objConn = New System.Data.OleDb.OleDbConnection(strCnn) objConn.Open() Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter() strSQL = &quot;Select Top 10 Title From Document&quot; objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn) objAdapter.Fill(objDataset) End If Dim oView As New DataView(objDataset.Tables(0)) DataGrid1.DataSource = oView DataGrid1.DataBind() objConn.Close() objConn.Dispose() objConn = Nothing If Request.QueryString(&quot;bExcel&quot;) = &quot;1&quot; Then Response.ContentType = &quot;application/vnd.ms-excel&quot; ' 從Content-Type header中去除charset設定 Response.Charset = &quot;&quot; ' 關閉 ViewState Me.EnableViewState = False Dim tw As New System.IO.StringWriter() Dim hw As New System.Web.UI.HtmlTextWriter(tw) ' 獲取control的HTML DataGrid1.RenderControl(hw) ' 把HTML寫回瀏覽器 Response.Write(tw.ToString()) Response.End() End If End Sub End Class </system></fo>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-988555/,如需轉載,請註明出處,否則將追究法律責任。

相關文章