Dream Tech Blog

A person with no dream in life can never live a happy life.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

将datagrid的数据源到出导excel

Posted on 2004-03-01 20:41  Dream  阅读(2701)  评论(8编辑  收藏  举报
Public Shared Sub Convert(ByVal ds As System.Data.DataSet, ByVal response As System.Web.HttpResponse)
        'first let's clean up the response.object
        response.Clear()
        response.Charset = ""
        'set the response mime type for excel
        response.ContentType = "application/vnd.ms-excel"
        'create a string writer
        Dim stringWrite As New System.IO.StringWriter
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
        'instantiate a datagrid
        Dim dg As New System.Web.UI.WebControls.DataGrid
        'set the datagrid datasource to the dataset passed in
        dg.DataSource = ds.Tables(0)
        'bind the datagrid
        dg.DataBind()
        'tell the datagrid to render itself to our htmltextwriter
        dg.RenderControl(htmlWrite)
        'all that's left is to output the html
        response.Write(stringWrite.ToString)
        response.End()
    End Sub