objConn = New System.Data.OleDb.OleDbConnection(strCnn) objConn.Open() Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter() strSQL = "Select Top 10 Title From Document" 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("bExcel") = "1" Then Response.ContentType = "application/vnd.ms-excel" ' 从Content-Type header中去除charset设置 Response.Charset = "" ' 关闭 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 |